我試圖通過ViewModel將DocumentViewer綁定到文檔,並且根本沒有成功。在MVVM中綁定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其他代碼()'
我的確發現奇怪的是,如果將文檔生成代碼從視圖模型構造函數放入視圖構造函數中,文檔就會正確顯示,我認爲這是一個有約束力的問題,但我在哪裏或如何不知道。
你在哪裏設置你的觀點作爲視圖模型的實例的DataContext的? –
我正在按照這篇文章設置資源文件中的DataContext - http://msdn.microsoft.com/en-us/magazine/dd419663.aspx 示例: DataTemplate> 我認爲這是行得通的,因爲我可以分解成DocumentViewer屬性 –
Rob