【问题标题】:How to show a flow document using a DocumentViewer?如何使用 DocumentViewer 显示流文档?
【发布时间】:2020-02-05 20:16:19
【问题描述】:

我的资源中有一个简单的流程文档,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" />

如何在DocumentViewer 中显示FlowDocument1.xaml

【问题讨论】:

    标签: wpf xaml flowdocument documentviewer


    【解决方案1】:

    首先,您不能将FlowDocument 添加到DocumentViewer,因为它只支持FixedDocument。您可以改用FlowDocumentScrollViewerFlowDocumentPageViewer

    <FlowDocumentScrollViewer x:Name="TestViewer"/>
    

    那么你必须在代码中设置Document属性:

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

    【讨论】:

    • 您当然可以将文档放入资源字典中,然后将其作为静态资源访问。
    【解决方案2】:
    <Grid>
        <Grid.Resources>
            <FlowDocument x:Key="YourFlowDoc">
                <Paragraph>
                    <TextBox Text="See it's Easy!"/>
                </Paragraph>
            </FlowDocument>
        </Grid.Resources>
        <FlowDocumentReader Document="{StaticResource YourFlowDoc}" />
    </Grid>
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      相关资源
      最近更新 更多