在 MVVM 中绑定 DocumentViewer
我试图通过 ViewModel 将 DocumentViewer 绑定到文档,但根本没有成功。
这是我的视图模型代码...
private DocumentViewer documentViewer1 = new DocumentViewer();
public DocumentViewerVM()
{
string fileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Here an xps document.xps");
XpsDocument document = new XpsDocument(fileName, FileAccess.Read);
documentViewer1.Document = document.GetFixedDocumentSequence();
document.Close();
}
public DocumentViewer DocumentViewer1
{
get
{ return documentViewer1; }
set
{
documentViewer1 = value;
OnPropertyChanged("DocumentViewer1");
}
}
这是视图中的 xaml...
<UserControl x:Class="DemoApp.View.DocumentViewerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid>
<DocumentViewer Name="DocumentViewer1" Document="{Binding Path=DocumentViewer1, UpdateSourceTrigger=PropertyChanged}" ></DocumentViewer>
</Grid>
</UserControl>
视图后面的代码不包含除“InitializeComponent()”以外的代码
我确实觉得奇怪的是,如果我将文档生成代码放在将视图模型构造函数放入视图构造函数中,文档会正确显示,这使我认为这是一个绑定问题,但我不知道在哪里或如何。
I'm trying to bind a DocumentViewer to a document via the ViewModel and am not succeeding at all.
Here is my view model code...
private DocumentViewer documentViewer1 = new DocumentViewer();
public DocumentViewerVM()
{
string fileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Here an xps document.xps");
XpsDocument document = new XpsDocument(fileName, FileAccess.Read);
documentViewer1.Document = document.GetFixedDocumentSequence();
document.Close();
}
public DocumentViewer DocumentViewer1
{
get
{ return documentViewer1; }
set
{
documentViewer1 = value;
OnPropertyChanged("DocumentViewer1");
}
}
here is the xaml in the view...
<UserControl x:Class="DemoApp.View.DocumentViewerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid>
<DocumentViewer Name="DocumentViewer1" Document="{Binding Path=DocumentViewer1, UpdateSourceTrigger=PropertyChanged}" ></DocumentViewer>
</Grid>
</UserControl>
the code behind for the view contains no code other than 'InitializeComponent()'
What I do find strange is that if I place the document generation code from the view model constructor into the view constructor the document is displayed correctly, this leads me to think it is a binding issue, but where or how I know not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于那些可能仍然不知道如何完成它的人。这是一个示例:
视图:
ViewModel:
For people who might still have no clue how to get it done. Here is an example:
The View:
The ViewModel:
您将
DocumentViewer
的Document
属性绑定到名为DocumentViewer1
的属性,该属性本身就是一个DocumentViewer
。Document
属性需要一个实现IDocumentPaginatorSource
的类型实例,例如 固定文档。You are binding the
Document
property of theDocumentViewer
to a property calledDocumentViewer1
which is itself aDocumentViewer
. TheDocument
property expects an instance of a type that implementsIDocumentPaginatorSource
, such as a FixedDocument.如果您希望保持视图模型原始状态并避免在视图模型库中包含PresentationCore.dll,请使用如下所示的WPF IValueConverter。
下面的 XAML 显示了如何使用上述转换器。此示例是一个数据模板,具有类型为 MessageBoxShowXpsDoc 的视图模型,该模型具有一个名为 DocumentPath 的简单字符串属性。该值被传递给转换器以获取 IDocumentPaginatorSource。
尽管包含完整视图模型超出了OP的范围,但这是我如何设置从视图模型传递到转换器的字符串路径的示例。
If you want to keep your view models pristine and avoid including PresentationCore.dll in your view model library, then use a WPF IValueConverter such as the following.
The XAML below shows how to use the above converter. This example is a data template that has a view model of Type MessageBoxShowXpsDoc which has a simple string property called DocumentPath. This is passed to the converter to obtain the IDocumentPaginatorSource.
Although including the full view model is outside the scope of the OP, this is an example of how I set that string path which is passed from the view model to the converter.
正如 devdigital(上面)已经解释的那样,类型的公共属性
需要
IDocumentPaginatorSource
。也许是这样的:
在您的 xaml 中,只需将其绑定到
DocumentViewer
Document
属性:As explained already by devdigital (above), a public property of type
IDocumentPaginatorSource
is needed.Something like this perhaps:
And in your xaml just bind this to the
DocumentViewer
Document
property: