将 XAML 导入 WPF RichTextBox

发布于 2024-07-11 18:41:40 字数 848 浏览 7 评论 0原文

我有一个在 WPF Web 服务中动态构建的 WPF RichTextBox。 此 Web 服务接受从第三方 silverlight RichTextBox 控件的内容中提取的 xaml 字符串。

<Paragraph TextAlignment=\"Left\"><Run FontFamily=\"Comic Sans MS\" FontSize=\"16\" Foreground=\"#FF0000FF\" FontWeight=\"Bold\" >This text is blue and bold.</Run></Paragraph>

如何将此 xaml 插入到我的 WPF RichTextBox 中? 我对 FlowDocument 和 Paragraph 和 Run 的概念有所了解,因此我可以使用下面的代码用文本填充 WPF RichTextBox,

        FlowDocument flowDocument = new FlowDocument();
        Paragraph par = new Paragraph();
        par.FontSize = 16;
        par.FontWeight = FontWeights.Bold;
        par.Inlines.Add(new Run("Paragraph text"));
        flowDocument.Blocks.Add(par);
        rtb.Document = flowDocument;

但我真的不想自己解析 xaml 来构建一个段落,因为它可以得到非常复杂。 有没有办法让控件知道如何解析传入的 xaml?

I have a WPF RichTextBox that is dynamically built in a WPF web service. This web service accepts a xaml string that is extracted from the contents of a third-party silverlight RichTextBox control.

<Paragraph TextAlignment=\"Left\"><Run FontFamily=\"Comic Sans MS\" FontSize=\"16\" Foreground=\"#FF0000FF\" FontWeight=\"Bold\" >This text is blue and bold.</Run></Paragraph>

How do I insert this xaml into my WPF RichTextBox? I somewhat understand the concepts of the FlowDocument and Paragraph and Run so I can populate the WPF RichTextBox with text using the code below,

        FlowDocument flowDocument = new FlowDocument();
        Paragraph par = new Paragraph();
        par.FontSize = 16;
        par.FontWeight = FontWeights.Bold;
        par.Inlines.Add(new Run("Paragraph text"));
        flowDocument.Blocks.Add(par);
        rtb.Document = flowDocument;

But what I really don't want to have to parse through the xaml myself to build a paragraph as it can get very complicated. Is there a way to just have the control know how to parse the passed in xaml?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2024-07-18 18:41:40

您可以使用 XamlReader 读取 Xaml 字符串并将其转换为控件:

string templateString = "<Paragraph xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"  TextAlignment=\"Left\"><Run FontFamily=\"Comic Sans MS\" FontSize=\"16\" Foreground=\"#FF0000FF\" FontWeight=\"Bold\" >This text is blue and bold.</Run></Paragraph>";
StringReader stringReader = new StringReader(templateString);
XmlReader xmlReader = XmlReader.Create(stringReader);
Paragraph template = (Paragraph)XamlReader.Load(xmlReader);

只需确保在模板中包含以下标记:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

HTH

You can use XamlReader to read your Xaml string and convert it to a control:

string templateString = "<Paragraph xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"  TextAlignment=\"Left\"><Run FontFamily=\"Comic Sans MS\" FontSize=\"16\" Foreground=\"#FF0000FF\" FontWeight=\"Bold\" >This text is blue and bold.</Run></Paragraph>";
StringReader stringReader = new StringReader(templateString);
XmlReader xmlReader = XmlReader.Create(stringReader);
Paragraph template = (Paragraph)XamlReader.Load(xmlReader);

Just make sure you include the following tag in your template:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文