How to show a flow document using a DocumentViewer?

纵饮孤独 提交于 2021-02-07 09:32:05

问题


I have a simple flow document in my resources, 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 x:Name="TestViewer" Document="Resources/FlowDocument1.xaml" />

How can I show FlowDocument1.xaml in a DocumentViewer?


回答1:


First you cannot add a FlowDocument to a DocumentViewer because it only supports FixedDocument. You may use FlowDocumentScrollViewer or FlowDocumentPageViewer instead.

<FlowDocumentScrollViewer x:Name="TestViewer"/>

Then you have to set the Document property in code:

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>


来源:https://stackoverflow.com/questions/17995533/how-to-show-a-flow-document-using-a-documentviewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!