2013-03-27 43 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。這會在正確的位置和正確的大小上繪製線條。

相關問題