如何使用DocumentViewer显示流文档?

时间:2013-08-01 13:32:39

标签: wpf xaml flowdocument documentviewer

我的资源中有一个简单的流程文档,FlowDocument1.xaml

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          ColumnWidth="400" FontSize="14" FontFamily="Georgia">
    <Paragraph>
        Test
    </Paragraph>
</FlowDocument>

我希望在DocumentViewer中显示此文档。我搜索了一个带路径的房产,但我找不到。以下引发异常:

<DocumentViewer x:Name="TestViewer" Document="Resources/FlowDocument1.xaml" />

如何在FlowDocument1.xaml中显示DocumentViewer

2 个答案:

答案 0 :(得分:4)

首先,您无法将FlowDocument添加到DocumentViewer,因为它仅支持FixedDocument。您可以改为使用FlowDocumentScrollViewerFlowDocumentPageViewer

<FlowDocumentScrollViewer x:Name="TestViewer"/>

然后你必须在代码中设置Document属性:

TestViewer.Document = Application.LoadComponent(
    new Uri("/Resources/FlowDocument1.xaml", UriKind.Relative)) as FlowDocument;

答案 1 :(得分:4)

<Grid>
    <Grid.Resources>
        <FlowDocument x:Key="YourFlowDoc">
            <Paragraph>
                <TextBox Text="See it's Easy!"/>
            </Paragraph>
        </FlowDocument>
    </Grid.Resources>
    <FlowDocumentReader Document={StaticResource YourFlowDoc}"/>
</Grid>