标签: fixeddocumentsequence

为什么我在打印到XpsDocument时丢失了数据绑定?

更新!

绑定工作.问题是XpsDocumentWriter没有正确编写FixedDocumentSequence的第一个文档的第一页.这似乎是很多人在做这类事情时遇到的问题(即全世界有五个开发人员).解决方案有点奇怪.我把它作为答案包括在内.


好吧,它比问题暗示的更微妙.

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

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

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

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

这里有一些示例代码来说明正在发生的事情:

// 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 …
Run Code Online (Sandbox Code Playgroud)

data-binding fixedpage xps fixeddocument fixeddocumentsequence

8
推荐指数
1
解决办法
6104
查看次数