Search code examples
c#wpfxamlflowdocumentflowdocumentscrollviewer

FlowDocument doesn't show my XAML code from code behind and instead shows it as HTML tags


I'm trying to show this XAML part in a Flowdocument

<Section xml:space='preserve' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Paragraph><Hyperlink NavigateUri='E6A88D2B.js'/></Paragraph><Paragraph /><Paragraph><Span Foreground='blue'><Run FontWeight='bold'>NOW, the</Run></Span><Span>/ˌen əʊ ˈdʌb<Run FontStyle='italic'>ə</Run>ljuː $ -oʊ-/ </Span><Run>BrE</Run><Run /><Run /><Run>AmE</Run><Run /><Run /><LineBreak /><Span><Span FontWeight='bold'><Run Foreground='blue'>(the National Organization for Women)</Run></Span> a large US organization started in 1966, which works for legal, economic, and social equality between women and men. Its first president was Betty ↑<Run>Friedan</Run>, who also helped to start it</Span><LineBreak /></Paragraph></Section>

When I insert my XAML Code here inside flowdocument tag it shows content perfectly and formatted:

<FlowDocumentScrollViewer Width="400" VerticalAlignment="Bottom" Height="200" >
        <FlowDocument>
            <Section xml:space='preserve' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Paragraph><Hyperlink NavigateUri='E6A88D2B.js'/></Paragraph><Paragraph /><Paragraph><Span Foreground='blue'><Run FontWeight='bold'>NOW, the</Run></Span><Span>/ˌen əʊ ˈdʌb<Run FontStyle='italic'>ə</Run>ljuː $ -oʊ-/ </Span><Run>BrE</Run><Run /><Run /><Run>AmE</Run><Run /><Run /><LineBreak /><Span><Span FontWeight='bold'><Run Foreground='blue'>(the National Organization for Women)</Run></Span> a large US organization started in 1966, which works for legal, economic, and social equality between women and men. Its first president was Betty ↑<Run>Friedan</Run>, who also helped to start it</Span><LineBreak /></Paragraph></Section>
        </FlowDocument>
    </FlowDocumentScrollViewer>

But I want to do this programaticaly from code behind and it doesn't work. And it shows unformated XAML text which is exactly the same as inserted XAML code

Paragraph paragraph = new Paragraph();
                    paragraph.Inlines.Add(new Run(myXamlCode));
                    Section section = new Section();
                    section.Blocks.Add(paragraph);
                    myFlowDocument.Blocks.Add(section);

What's the best approach to show my XAML code?


Solution

  • you may need to parse the xaml to the appropriate object instead of inserting the same as a string value inside a Run.

    XamlReader.Parse helps you to parse such string and initialize/create object for the same.

        Section section = XamlReader.Parse(myXamlCode) as Section;
        myFlowDocument.Blocks.Add(section);
    

    above example is assuming the string myXamlCode with the following text (as mentioned in question)

    <Section xml:space='preserve' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Paragraph><Hyperlink NavigateUri='E6A88D2B.js'/></Paragraph><Paragraph /><Paragraph><Span Foreground='blue'><Run FontWeight='bold'>NOW, the</Run></Span><Span>/ˌen əʊ ˈdʌb<Run FontStyle='italic'>ə</Run>ljuː $ -oʊ-/ </Span><Run>BrE</Run><Run /><Run /><Run>AmE</Run><Run /><Run /><LineBreak /><Span><Span FontWeight='bold'><Run Foreground='blue'>(the National Organization for Women)</Run></Span> a large US organization started in 1966, which works for legal, economic, and social equality between women and men. Its first president was Betty ↑<Run>Friedan</Run>, who also helped to start it</Span><LineBreak /></Paragraph></Section>


    as a side note, the code in question translates to the following

        <FlowDocument>
            <Section>
                <Paragraph>
                    <Run Text="&lt;Section&gt;...&lt;/Section&gt;" />
                </Paragraph>
            </Section>
        </FlowDocument>
    

    this may render like the html thing you see

    eg

    <Section>...</Section>

    instead of the one which you expect.