wpf 如何使用 DocumentViewer 显示流文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17995533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:20:34  来源:igfitidea点击:

How to show a flow document using a DocumentViewer?

wpfxamlflowdocumentdocumentviewer

提问by ?afak Gür

I have a simple flow document in my resources, FlowDocument1.xaml:

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

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

And I want to show this document in a DocumentViewer. I searched for a property that takes path but I couldn't find one. And the following throws an exception:

我想在一个DocumentViewer. 我搜索了一个需要路径的属性,但我找不到。并且以下抛出异常:

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

How can I show FlowDocument1.xamlin a DocumentViewer?

我怎样才能FlowDocument1.xaml在 a 中显示DocumentViewer

回答by Clemens

First you cannot add a FlowDocumentto a DocumentViewerbecause it only supports FixedDocument. You may use FlowDocumentScrollVieweror FlowDocumentPageViewerinstead.

首先,您不能将 a 添加FlowDocument到 a,DocumentViewer因为它仅支持FixedDocument. 您可以使用FlowDocumentScrollViewerFlowDocumentPageViewer代替。

<FlowDocumentScrollViewer x:Name="TestViewer"/>

Then you have to set the Documentproperty in code:

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

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

回答by bRySaGeeK

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