data-binding - 为什么我在打印到 XpsDocument 时丢失了数据绑定(bind)?

标签 data-binding xps fixedpage fixeddocument fixeddocumentsequence

更新!

装订工作。问题是 XpsDocumentWriter 没有正确写入 FixedDocumentSequence 的第一个文档的第一页。这似乎是很多人在做这种事情时遇到的问题(即全局有五个开发人员)。解决方案有点奇怪。我把它作为一个答案。

好的,它比问题所暗示的要微妙一些。

我有一系列 FixedPages,每个都有其 DataContext 单独设置。每个 FixedPage 还具有一个或多个绑定(bind)到上下文的控件。

如果我将这些 FixedPages 添加到单个 FixedDocument 并将此单个 FixedDocument 写入 XpsDocument,我的绑定(bind)将被取消引用(可以这么说)并且正确的值会显示在 XpsDocument 中。

如果我将这些 FixedPages 添加到各个 FixedDocuments(每个 FP 被添加到一个新的 FD),然后这些 FixedDocuments 被添加到一个 FixedDocumentSequence,然后这个序列被写入 XpsDocument,我的绑定(bind)不会被取消引用并且我的 FixedPages 显示为空白.

调试告诉我我没有丢失我的绑定(bind)或我的绑定(bind)上下文,所以这不是导致此失败的原因。

下面是一些示例代码来说明发生了什么:

// This works
FixedPage fp = CreateFixedPageWithBinding();
fp.DataContext = CreateDataContext();
// Add my databound fixed page to a new fixed document
var fd = new FixedDocument();
var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fd.Pages.Add(pageContent);
// Create an xps document and write my fixed document to it
var p = Package.Open("c:\\output.xps", FileMode.CreateNew);
var doc = new XpsDocument(p);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
wri2.Write(fd);
p.Flush();
p.Close();

// This does NOT work
FixedPage fp = CreateFixedPageWithBinding();
fp.DataContext = CreateDataContext();
// Add my databound fixed page to a new fixed document
var fd = new FixedDocument();
var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fd.Pages.Add(pageContent);
// Create a fixed document sequence and add the fixed document to it
FixedDocumentSequence fds = CreateFixedDocumentSequence();
var dr = new DocumentReference();
dr.BeginInit();
dr.SetDocument(fd);
dr.EndInit();
(fds as IAddChild).AddChild(dr);
// Create an xps document and write the fixed document sequence to it
var p = Package.Open("c:\\output.xps", FileMode.CreateNew);
var doc = new XpsDocument(p);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
wri2.Write(fds);
p.Flush();
p.Close();

您可以看到两者之间的唯一区别是我将固定文档添加到固定文档序列中,然后将其写入。

显然,当我的固定文档未写入 Xps 文档时,不会发生任何导致评估数据绑定(bind)和插入绑定(bind)值的魔法。我需要能够编写多个固定文档,并且 Write 方法只能调用一次,因此需要我将 FixedDocuments 添加到我然后编写的 FixedDocumentSequence 中。但我也需要我该死的数据绑定(bind)才能工作!

在这种情况下的任何帮助将不胜感激。我知道它不完全是框架中最常见的部分。我只是希望这里有人对此有一些操作经验(我在看着你,潜伏着 MS 员工)。

最佳答案

这个错误的原因是 FixedPage 的布局在写入之前没有被更新。这会导致 FixedDocumentSequence 中第一个 FixedDocument 中的第一个 FixedPage 写入不正确。这会影响 结果文档中没有其他页面 ,这使得这个错误/边缘案例更难确定。

以下 WORKS(非工作示例的重写版本):

FixedPage fp = CreateFixedPageWithBinding();
fp.DataContext = CreateDataContext();
var fd = new FixedDocument();

/* PAY ATTENTION HERE */
// set the page size on our fixed document 
fd.DocumentPaginator.PageSize =
   new System.Windows.Size()
   {
       Width = DotsPerInch * PageWidth,
       Height = DotsPerInch * PageHeight
   };
// Update the layout of our FixedPage
var size = fd.DocumentPaginator.PageSize;
page.Measure(size);
page.Arrange(new Rect(new Point(), size));
page.UpdateLayout();    
/* STOP PAYING ATTENTION HERE */

var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fd.Pages.Add(pageContent);
// Create a fixed document sequence and add the fixed document to it
FixedDocumentSequence fds = CreateFixedDocumentSequence();
var dr = new DocumentReference();
dr.BeginInit();
dr.SetDocument(fd);
dr.EndInit();
(fds as IAddChild).AddChild(dr);
// Create an xps document and write the fixed document sequence to it
var p = Package.Open("c:\\output.xps", FileMode.CreateNew);
var doc = new XpsDocument(p);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
wri2.Write(fds);
p.Flush();
p.Close();

关于data-binding - 为什么我在打印到 XpsDocument 时丢失了数据绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/449036/

相关文章:

c# - WPF:如何绑定(bind)到另一个控件绑定(bind)的属性?

wpf - 如何推迟对 WPF 中的绑定(bind)的更新

wpf - FixedDocument中的WPF表

c# - 如何通过 VS.NET 自动化将文件打印到 C# 或更好的 XpsDocumentWriter?

WPF DocumentViewer 查找功能和固定页面文档

c# - WPF 绑定(bind)到图像(位图)转换器未显示

c# - 具有只读属性的数据绑定(bind)

c# - 使用 ITextSharp 生成 XPS 文档而不是 PDF?

printing - ReportViewer - 设置默认文件名