我如何引用我的FlowDocument

时间:2010-11-12 14:35:05

标签: wpf flowdocument

我在WPF控件库中定义了一个FlowDocument(Add New Item ...,FlowDocument - 是文件的根元素)。我打算在多个上下文中使用它,例如在用户控件或窗口中,在代码中引用数据绑定和导出到xps等。但我无法弄清楚如何获取对本文档的实例。它似乎没有在编译的程序集中创建一个对象。

更具体地说,这是我的问题

<MyUserControl ........ >
  <FlowDocumentScrollViewer>
    <!-- doesn't work --><namespaceRef:MyFlowDocument /> 
  <FlowDocumentScrollViewer>
</MyUserControl>

1 个答案:

答案 0 :(得分:2)

最简单的解决方案可能是将FlowDocument放在资源字典中,然后像这样使用x:Key

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <FlowDocument x:Key="myFlowDocument" ..>
    </FlowDocument>
</ResourceDictionary>

<FlowDocumentScrollViewer Name="flowDocumentScrollViewer">
    <StaticResource ResourceKey="myFlowDocument"/>
</FlowDocumentScrollViewer>

否则你必须将FlowDocument设置为构建动作嵌入式资源,并在代码后面使用类似的东西将其编码

Stream flowDocumentStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DocumentNameSpace.FlowDocumentName.xaml");
FlowDocument flowDocument = (FlowDocument)XamlReader.Load(flowDocumentStream);
flowDocumentScrollViewer.Document = flowDocument;

<强>更新
我想如果你想查看它,可能会使用ObjectDataProvider来加载FlowDocument。无论如何,ResourceDictionary似乎是最简单的出路。