Embed custom control in WPF FlowDocument

2019-06-09 05:22发布

Is there any way to embed a custom control into the flowdocument and have it correctly displayed by the FlowDocument viewers (export it to Xaml text file and open it by a viewer)?

2条回答
戒情不戒烟
2楼-- · 2019-06-09 05:40

If by custom control you mean UserControl or any custom control inherited from a WPF control, you can't: This custom control is in its own XML namespace, and requires code deployed in an assembly. Your XAML reader won't have access to it when attempting to parse the file, and won't succed.

You can only use controls provided by Microfost, and deployed with the viewer. Maybe it's possible to make viewers know of assemblies deployed in the GAC, but it's a solution only if you can deploy in the client GAC.

查看更多
孤傲高冷的网名
3楼-- · 2019-06-09 05:45

yes use a BlockUIContainer or InlineUIContainer

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:l="clr-namespace:MyNamespace;assembly=MyAssembly">
    <BlockUIContainer>
        <l:MyCustomControl/>
    </BlockUIContainer>
</FlowDocument>

note that wherever your viewer is will need to have access + trust to use the assembly with the custom control in. The easiest way to achieve this is to have the viewer in the same assembly as the the control.

查看更多
登录 后发表回答