我创建了一个自定义文档分页器,它采用数据表并完全按照我的需要打印。我想做一个打印预览。我已阅读有关如何在内存中创建 xps 文件然后显示它的所有帖子。我只是无法让它工作。这是我的代码。我正在使用 MVVM 模式。请注意代码行 _data.DocView=fds; 这会将数据传递给我的视图模型。
PrintDialog dialog = new PrintDialog();
dialog.ShowDialog();
StoreDataSetPaginator paginator = new StoreDataSetPaginator(dt, new Typeface("Calibri"), 8, 96 * 0.75,
new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
//this is commented out to attempt the print preview
// dialog.PrintDocument(paginator, "Print out");
MemoryStream ms = new MemoryStream();
Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
Uri DocumentUri = new Uri("pack://InMemoryDocument.xps");
PackageStore.AddPackage(DocumentUri, package);
XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.NotCompressed,
DocumentUri.AbsoluteUri);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(paginator);
IDocumentPaginatorSource fds = xpsDocument.GetFixedDocumentSequence();
_data.DocView = fds;
PrintPreviewConduit prntv = new PrintPreviewConduit();
prntv.Show();
现在这是我的视图模型:
private IDocumentPaginatorSource _docView;
public IDocumentPaginatorSource DocView
{
get { return _docView; }
set
{
_docView = value;
OnPropertyChanged("DocView");
}
}
最后是我的 XAML:
<Grid>
<DocumentViewer Name="docview" Document="{Binding DocView}"/>
</Grid>
我在“public IDocumentPaginatorSource DocView”的 ViewModel 中输入了一个断点,当我将鼠标悬停在它上面时,我得到“System.Windows.Documents.FixedDocumentSequence。不知道我应该得到什么。我现在花了很长时间和任何帮助将不胜感激。系统