0
我試圖將用戶控件的頁面打印出來。它工作得很好,除了第一頁首次打印的用戶控件的數據綁定問題
在這裏爲我打印的代碼:
#region Print
/// <summary>
/// Used the XpsDocumentWriter to write a FixedDocumentSequence which contains the UIElements as single pages
/// </summary>
/// <param name="xpsWriter"></param>
/// <param name="uiElements"></param>
private void PrintUIElements(XpsDocumentWriter xpsWriter, List<UIElement> uiElements)
{
FixedDocumentSequence fixedDocSeq = new FixedDocumentSequence();
foreach (UIElement element in uiElements)
(fixedDocSeq as IAddChild).AddChild(toDocumentReference(element));
// write the FixedDocumentSequence as an XPS Document
xpsWriter.Write(fixedDocSeq);
}
/// <summary>
/// encapsulater for a UIElement in an DocumentReference
/// DocumentReference(FixedDocument(PageContent(FixedPage(UIElement))))
/// to simplify the print of multiple pages
/// </summary>
/// <param name="uiElement">the UIElement which</param>
/// <returns>creates a DocumentReference</returns>
private DocumentReference toDocumentReference(UIElement uiElement)
{
if (uiElement == null)
throw new NullReferenceException("the UIElement has to be not null");
FixedPage fixedPage = new FixedPage();
PageContent pageContent = new PageContent();
FixedDocument fixedDoc = new FixedDocument();
DocumentReference docRef = new DocumentReference();
#region Step1
// add the UIElement object the FixedPage
fixedPage.Children.Add(uiElement);
#endregion
#region Step2
// add the FixedPage to the PageContent collection
pageContent.BeginInit();
((IAddChild)pageContent).AddChild(fixedPage);
pageContent.EndInit();
#endregion
#region Step 3
// add the PageContent to the FixedDocument collection
((IAddChild)fixedDoc).AddChild(pageContent);
#endregion
#region Step 4
// add the FixedDocument to the document reference collection
docRef.BeginInit();
docRef.SetDocument(fixedDoc);
docRef.EndInit();
#endregion
return docRef;
}
#endregion
,我使用它像這樣
var pDialog = new PrintDialog();
if (pDialog.ShowDialog() == true)
{
List<UIElement> list = new List<UIElement>();
foreach (CostumerVM item in Itemlist.Where(item => item.isChecked == true))
{
var vm = new CostumerpageVM(item.VName, item.NName, item.DebNr, item.Original.Id, plan, User.Einrichtungen.refSpeiseplantypId, selectedKW.Key);
var window = new PageV{ DataContext = vm };
list.Add(window);
}
var xpsDocWriter = PrintQueue.CreateXpsDocumentWriter(pDialog.PrintQueue);
PrintUIElements(xpsDocWriter, list);
}
比我創建了一個小測試版本來檢查我打印方法和結果是工作正常,所以我想好也許我的LazyLoad是問題,所以我創建了一個初始化方法
// in my CostumerpageVM
public void Init()
{
var properties = this.GetType().GetProperties();
foreach (var p in properties)
{
if (p.Name != "Item") // is part of the IDataError and it must not be called
{ var a = p.GetValue(this, null); }
}
}
但我仍然面臨同樣的問題,因此,進一步的建議,將不勝感激
我也是媒體鏈接檢查是什麼
- this solution for binding errors(不知道如何在我的情況下實現)
- 添加在我PageV加載事件,這不被調用