Embed custom control in WPF FlowDocument

2019-06-09 05:42发布

问题:

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)?

回答1:

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.



回答2:

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.