WPF FlowDocument绑定 [英] WPF FlowDocument Binding

查看:136
本文介绍了WPF FlowDocument绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft的 XAML/HTML转换器来转换HTML从数据库转换为XAML字符串.Microsoft转换器似乎正在正确设置文本格式,但是将输出绑定到XAML对象时遇到了麻烦.

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.

例如,使用以下HTML:

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>

我将获得XAML输出:

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>

假设HTML作为数据库对象的文本"属性进入WPF应用程序,然后我像这样使用Binding和Converters:

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}}" />

不幸的是,这只会将XAML打印到页面上,而不会解析它.我假设这是因为我绑定到了TextBlock,这是预期的结果.我的问题是如何绑定此输出与FlowDocument相关的控件(如Paragraph,Run,Section等)?

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?

注意:我意识到有很多专用于将HTML转换为XAML的线程.我已经引用了其中大多数,但是在此特定步骤上都缺少它们.感谢您提供任何帮助或链接.

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.

推荐答案

在您的示例中,您将xaml作为文本并绑定到Text-property.这只会将xaml显示为文本.

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

如果有直接方法将其绑定为FlowDocument的内容,我不知道.IMO由于FlowDocument的结构,这是不可能的.但是也许有人知道一种方法,并为您发布了解决方案.

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.

希望这会有所帮助.

这篇关于WPF FlowDocument绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆