2013-03-27 42 views
2

我正在从我们的Silverlight应用程序打印一组标签。构成数据的数据是从数据库中读取的,UI元素在运行中创建并添加到Canvas进行布局。标签在网页上以网格布置,行数和列数由所使用的纸材确定。线条元素不出现在打印的第二页和后续页面

Label on page one of print

Label on page two of print

一切从线到“重拳出击”的元素(例如原价当一个项目在售)的加入除了做工精细这是生成行的代码:

var line = new Line { StrokeThickness = 2, Stroke = new SolidColorBrush(Colors.Black) }; 
line.X1 = 0; 
line.SetBinding(Line.Y1Property, new Binding { ElementName = element.Name, Path = new PropertyPath("ActualHeight") }); 
line.Y2 = 0; 
line.SetBinding(Line.X2Property, new Binding { ElementName = element.Name, Path = new PropertyPath("ActualWidth") }); 
// Insert the element straight after the element it's bound to 
canvas.Children.Insert(canvas.Children.IndexOf(element) + 1, line); 
line.SetValue(Canvas.TopProperty, element.GetValue(Canvas.TopProperty)); 
line.SetValue(Canvas.LeftProperty, element.GetValue(Canvas.LeftProperty)); 
// and make sure it's Z index is always higher 
line.SetValue(Canvas.ZIndexProperty, (int)element.GetValue(Canvas.ZIndexProperty) + 1); 
  • canvas是用于显示标签的帆布
  • element是要伸出的元素(在这种情况下是原始价格)。

  1. 代码获取调用用于被印刷的所有标签。
  2. 绑定是一致的。
  3. 如果我使用硬编码值替换绑定,则该行会被绘制,因此它看起来是由绑定中的某些内容引起的。但是:
  4. “父”元素的ActualHeightActualWidth对于每个标签都是相同的。
  5. 该行不是在其他地方打印出来的(我可以看到)。如果我在第一页停止输出,则不显示任何行。
  6. 其他一切正在出现并出现在正确的位置。

我错过了什么?

回答

1

它似乎是错误的绑定。无论我在将行中的绑定添加到行后消失了什么 - 在某些情况下甚至从第一页开始。

最终只有工作的事情是更改代码这样:

element.Measure(new Size(canvas.Width, canvas.Height)); 
var line = new Line { StrokeThickness = 2, Stroke = new SolidColorBrush(Colors.Black) }; 
line.X1 = 0.0; 
line.Y1 = element.ActualHeight; 
line.Y2 = 0.0; 
line.X2 = element.ActualWidth; 
// Insert the element straight after the element it's bound to 
canvas.Children.Insert(canvas.Children.IndexOf(element) + 1, line); 
line.SetValue(Canvas.TopProperty, element.GetValue(Canvas.TopProperty)); 
line.SetValue(Canvas.LeftProperty, element.GetValue(Canvas.LeftProperty)); 
// and make sure it's Z index is always higher 
line.SetValue(Canvas.ZIndexProperty, (int)element.GetValue(Canvas.ZIndexProperty) + 1); 
line.Height = element.ActualHeight; 
line.Width = element.ActualWidth; 

所以我“措施”的文本元素,以确保它的高度和宽度进行更新,然后设置Y1X2,HeightWidth属性直接来自文本元素的ActualHeightActualWidth。这会在正确的位置和正确的大小上绘制线条。

相关问题