首次打印的UserControl的DataBinding问题

本文关键字:DataBinding 问题 UserControl 打印 | 更新日期: 2023-09-27 18:21:41

我正在尝试将用户控件打印为页面。除了第一页外,它运行得很好

这是我打印的代码:

    #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); }
        }
    }

但我仍然面临同样的问题,因此将非常感谢进一步的建议

我还准备好检查

  • 这个绑定错误的解决方案(在我的情况下不知道如何实现)
  • 在我的PageV中添加了一个Loaded事件,没有被调用

首次打印的UserControl的DataBinding问题

好吧,我意外地修复了它:)在阅读了更多关于FixedDocument的内容后,我改变了打印方法,我的绑定不再在空间中丢失

    /// <summary>
    /// Used the XpsDocumentWriter to write a FixedDocument which get created and contains the UIElements as single pages
    /// </summary>
    /// <param name="xpsWriter">XpsDocumentWriter</param>
    /// <param name="uiElements">List of UIElement</param>
    private void PrintUIElements(XpsDocumentWriter xpsWriter, List<UIElement> uiElements)
    {
        var fixedDoc = new FixedDocument();
        foreach (UIElement element in uiElements)
        {
            var fixedPage = new FixedPage();
            var pageContent = new PageContent();
            // add the UIElement object the FixedPage
            fixedPage.Children.Add(element);
            // add the FixedPage object the PageContent
            pageContent.Child = fixedPage;
            // add the PageContent object the FixedDocument
            fixedDoc.Pages.Add(pageContent);
        }
        xpsWriter.Write(fixedDoc);
    }