将 XAML 导入 WPF RichTextBox
我有一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 XamlReader 读取 Xaml 字符串并将其转换为控件:
只需确保在模板中包含以下标记:
HTH
You can use XamlReader to read your Xaml string and convert it to a control:
Just make sure you include the following tag in your template:
HTH