我试图通过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()‘以外的任何代码
我确实发现奇怪的是,如果我将视图模型构造函数中的文档生成代码放入视图构造函数中,文档就会正确显示,这使我认为这是一个绑定问题,但我不知道是在哪里或以何种方式。
发布于 2011-10-10 07:27:40
您将DocumentViewer的Document属性绑定到一个名为DocumentViewer1的属性,该属性本身就是一个DocumentViewer。Document属性需要实现IDocumentPaginatorSource的类型的实例,如FixedDocument。
发布于 2018-07-06 00:33:50
如果您希望保持视图模型的原始状态,并避免在视图模型库中包含PresentationCore.dll,那么可以使用如下所示的WPF IValueConverter。
namespace Oceanside.Desktop.Wpf.Dialogs.Converters
{
using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <inheritdoc />
/// <summary>
/// Our view models contain string paths to all XPS documents that we want to show. However,
/// the DocumentViewer.Document property must be of type IDocumentPaginatorSource which we do
/// not want to include in our view model because it will tie our view models to the
/// PresentationCore.dll. To assure all view logic and libraries are kept separate from our
/// view model, this converter to take a string path and convert it to a
/// FixedDocumentSequence which implements the IDocumentPaginatorSource interface.
/// </summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
[ValueConversion(typeof(string), typeof(IDocumentPaginatorSource))]
public sealed class DocumentPaginatorSourceConverter : IValueConverter
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <inheritdoc />
////////////////////////////////////////////////////////////////////////////////////////////////////
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (!(value is string xpsFilePath)) return null;
var document = new XpsDocument(xpsFilePath, FileAccess.Read);
var source = document.GetFixedDocumentSequence();
document.Close();
return source;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <inheritdoc />
/// <summary>This function is not supported and will throw an exception if used.</summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
//We have no use to convert from IDocumentPaginatorSource to a string path.
throw new NotSupportedException("Unable to convert an IDocumentPaginatorSource to a string path.");
}
}
}下面的XAML展示了如何使用上面的转换器。这个示例是一个数据模板,它有一个MessageBoxShowXpsDoc类型的视图模型,该视图模型有一个名为DocumentPath的简单字符串属性。这将传递给转换器以获取IDocumentPaginatorSource。
<!-- For showing xps/flow docs such as customer receipts -->
<DataTemplate
DataType="{x:Type dialogVms:MessageBoxShowXpsDoc}"
xmlns:converters="clr-namespace:Oceanside.Desktop.Wpf.Dialogs.Converters">
<DataTemplate.Resources>
<converters:DocumentPaginatorSourceConverter x:Key="DocPagConverter" />
</DataTemplate.Resources>
<DocumentViewer Document="{Binding DocumentPath,
Converter={StaticResource DocPagConverter}}" />
</DataTemplate>尽管包含完整视图模型超出了OP的范围,但这是我如何设置从视图模型传递到转换器的字符串路径的一个示例。
var viewModel = MessageBoxShowXpsDoc {DocumentPath = @"TestData\sample.xps"};发布于 2017-01-05 19:27:33
正如上面的devdigital所解释的那样,需要一个类型为IDocumentPaginatorSource的公共属性。
可能是这样的:
private IDocumentPaginatorSource _fixedDocumentSequence;
public IDocumentPaginatorSource FixedDocumentSequence
{
get { return _fixedDocumentSequence; }
set
{
if (_fixedDocumentSequence == value) return;
_fixedDocumentSequence = value;
OnPropertyChanged("FixedDocumentSequence");
}
}在您的xaml中,只需将其绑定到DocumentViewer Document属性:
<Grid>
<DocumentViewer
Name="DocViewer"
Document="{Binding FixedDocumentSequence}"/>
</Grid>https://stackoverflow.com/questions/7696859
复制相似问题