开发者

WPF FlowDocument Binding

开发者 https://www.devze.com 2023-01-15 18:38 出处:网络
I am using Microsoft\'s XAML/HTML conv开发者_如何学Pythonerter to convert HTML from a database into a XAML string. The Microsoft converter seems to be formatting the text correctly, but I\'m having tr

I am using Microsoft's XAML/HTML conv开发者_如何学Pythonerter to convert HTML from a database into a XAML string. The Microsoft converter seems to be formatting the text correctly, but I'm having trouble binding the output to a XAML object.

For example, using the following HTML:

<span style="font-weight: bold; font-family: Georgia; color: rgb(0, 96, 144); text-decoration: underline;">Hello world.</span>

I will get the XAML output:

<Section xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph>
    <Run FontWeight="bold" TextDecorations="Underline" FontFamily="georgia">Hello world.</Run>
</Paragraph>

Assuming the HTML is coming into the WPF application as the "Text" property of a database object, I then use Binding and Converters like so:

<TextBlock Text="{Binding Path=ActiveDataItem.Text, Converter={StaticResource convertHTMLToXaml}}" />

Unfortunately, this just prints the XAML to the page and doesn't parse it. I'm assuming this is because I'm binding to the TextBlock and that's the expected outcome. My question is how do I bind this output a FlowDocument related control like a Paragraph, Run, Section, or whatever?

Note: I realize there a quite a few threads dedicated to converting HTML to XAML. I have referenced most of them, but they are all lacking on this specific step. Any help or links are appreciated, thanks in advance.


For your example, you have the xaml as text and bound to the Text-property. This only shows the xaml as text.

If there is a direct way to bind it as the content of a FlowDocument, I don't known. IMO this is not possible due to the structure of FlowDocument. But maybe someone knows a way and posts you a solution for this.

To do it manually, look at the example of this page (dead link, see Archive.org). There I have seen that the author loads a XAML-string into a RichTextBox. You can change the code to your needs (RichtTextBox works also with FlowDocs). Search for public static class RichTextboxAssistant, there is the code your looking for. Take care for the encoding. He uses ASCII. Maybe you have to change this to UTF.

Hope this helps.


  • For .NET versions previous to 4.0: In this link, Vincent Van Den Berghe explain how to extend FlowDocument to support 'Bindable Runs', check it out

  • For .NET 4.0: Run.Text property is bindable

0

精彩评论

暂无评论...
验证码 换一张
取 消