我可以将现有的WPF(XAML)控件进行数据绑定,然后将其转换为XPS文档,并使用WPF XPS文档查看器显示和打印吗?如果可以,如何实现?如果不行,那么我应该如何在WPF中使用XPS/PDF等来进行报表生成呢?基本上,我想要将现有的WPF控件进行数据绑定,以获取其中的有用数据,然后使它可打印和可保存给最终用户。理想情况下,文档的创建应该是在内存中完成的,除非用户明确地保存了该文档,否则不需要在磁盘上进行操作。这种方案可行吗?
在研究许多不同的示例之后,我发现它们都非常复杂,需要使用文档编写器、容器、打印队列和打印票据,然而我找到了Eric Sink关于WPF中的打印的文章。
这篇文章提供的简化代码仅有10行。
public void CreateMyWPFControlReport(MyWPFControlDataSource usefulData)
{
//Set up the WPF Control to be printed
MyWPFControl controlToPrint;
controlToPrint = new MyWPFControl();
controlToPrint.DataContext = usefulData;
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
//Create first page of document
fixedPage.Children.Add(controlToPrint);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
//Create any other required pages here
//View the document
documentViewer1.Document = fixedDoc;
}
我的示例相当简单,它不包括页面大小和方向,其中包含一组完全不符合预期的问题。 也没有任何保存功能,因为微软似乎忘记在文档查看器中包含保存按钮。
保存功能相对简单(也来自Eric Sinks文章)
public void SaveCurrentDocument()
{
// Configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "MyReport"; // Default file name
dlg.DefaultExt = ".xps"; // Default file extension
dlg.Filter = "XPS Documents (.xps)|*.xps"; // Filter files by extension
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Save document
string filename = dlg.FileName;
FixedDocument doc = (FixedDocument)documentViewer1.Document;
XpsDocument xpsd = new XpsDocument(filename, FileAccess.ReadWrite);
System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.Write(doc);
xpsd.Close();
}
}
答案是肯定的,你可以取一个现有的WPF (XAML)控件,将其数据绑定,并将其转换为XPS文档 - 这并不难。