views:

34

answers:

1

I am using Microsoft's XAML/HTML converter 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"&gt;
<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.

+1  A: 

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. 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.

HCL
I ended up using a version of that RichTextboxAssistant class to extend the Document property to be a Dependency property so that I could bind to it. I then had to remove the border and background color of the RichTextEditor so the text appeared to be on a background and not inside a TextBox. Not the greatest solution, but it worked!
Phil Scholtes