[英]Print preview using custom paginator and XpsDocumentWriter not displaying in DocumentViewer
I have created a custom document paginator that takes a datatable and prints exactly as I need. 我创建了一个自定义文档分页器,该分页器接受一个数据表并完全按照我的需要进行打印。 I would like to do a print preview.
我想做一个打印预览。 I have read all the posts on how to create a xps file in memory and then display it.
我已经阅读了有关如何在内存中创建xps文件然后显示它的所有文章。 I just can't get it to work.
我就是无法正常工作。 Here is my code.
这是我的代码。 I am using a MVVM pattern.
我正在使用MVVM模式。 Please note the line of code _data.DocView=fds;
请注意代码行_data.DocView = fds; This passes the data to my view model.
这会将数据传递给我的视图模型。
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();
Now here is my view model: 现在这是我的视图模型:
private IDocumentPaginatorSource _docView;
public IDocumentPaginatorSource DocView
{
get { return _docView; }
set
{
_docView = value;
OnPropertyChanged("DocView");
}
}
And finally my XAML: 最后是我的XAML:
<Grid>
<DocumentViewer Name="docview" Document="{Binding DocView}"/>
</Grid>
I entered a break point in my ViewModel at "public IDocumentPaginatorSource DocView" and when I roll my mouse over it I get "System.Windows.Documents.FixedDocumentSequence. Not sure what i should be getting. I spent a good while now and any help would be greatly appreciated. Sys 我在ViewModel的“ public IDocumentPaginatorSource DocView”上输入了一个断点,当我将鼠标悬停在该断点上时,我得到了“ System.Windows.Documents.FixedDocumentSequence。不确定我应该得到什么。我花了好一阵子了,现在得到了任何帮助将不胜感激。
Well I feel stupid. 好吧,我觉得很蠢。 I did not set the new window's datacontext to my view model.
我没有将新窗口的数据上下文设置为我的视图模型。 Now everything works!!!!
现在一切正常!!!
PrintPreviewConduit prntv = new PrintPreviewConduit();
prntv.DataContext = _data;
_data.DocView = fds;
prntv.Show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.