如何将XAML文本放入richtextblock?

时间:2014-02-08 06:12:44

标签: c# html binding winrt-xaml richtextblock

我从网络加载数据,然后使用库Html2Xaml从生成的HTML文件生成XAML并将生成的字符串绑定到RichTextBlock.DataContext,但是当您打开页面为空时,我做错了什么?< / p>

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <RichTextBlock>
       <RichTextBlock.DataContext>
            {Binding Content}
       </RichTextBlock.DataContext>
    </RichTextBlock>
</ScrollViewer>

内容变得如此变化:

var item = await SampleDataSource.GetItemAsync((String)e.NavigationParameter);
HtmlDocument link_page = new HtmlDocument();
link_page.LoadHtml(await LoadPage(item.UniqueId));
HtmlNode _table2 = link_page.GetElementbyId("main_body");
item.Content = Html2XamlConverter.Convert2Xaml(_table2.WriteContentTo());
//Content now looks like this: "<Paragraph LineStackingStrategy="MaxHeight">Процедурная генерация карты (часть 1)</Paragraph><Paragraph  LineStackingStrategy="MaxHeight"></Paragraph><Paragraph  LineStackingStrategy="MaxHeight">Автор: <Bold>Артем Гуревич</Bold></Paragraph><Paragraph  LineStackingStrategy="MaxHeight">..."
this.DefaultViewModel["Item"] = item;

1 个答案:

答案 0 :(得分:0)

如果您的HTMLToXaml转换逻辑正常,并且您的DataContext设置正确,那么问题是您不会影响RichTextBox的文本,设置DataContext是不够的,您应该这样做:

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <RichTextBlock>
       <RichTextBlock.DataContext>
            {Binding Content}
       </RichTextBlock.DataContext>
       <FlowDocument>
            <Paragraph>
                <!--your content should be here-->
            </Paragraph>
       </FlowDocument>
    </RichTextBlock>
</ScrollViewer>

您可以根据需要添加多个Paragraph,它们只需要在FlowDocument内,因此您应该从item.Content中提取每个段落的值并将其作为段落添加到FlowDocument:

        Paragraph myParagraph = new Paragraph();
        myParagraph.Inlines.Add(ParagraphValueFromItemContent);

        // Add the paragraph to the FlowDocument.
        myFlowDoc.Blocks.Add(myParagraph);