我正在使用RichTextBox
显示一些我必须动态标记的只读文本。
如何在文本中传递标记代码以使其由RichTextBox
控件呈现?
例如,我想将this is \cf6 sample \cf1 text
传递给richtextbox以进行渲染。
现在,我构建一个FlowDocument
并将文本值添加到运行对象,但文本将按字面呈现。
RichTextBox fieldLabel = new RichTextBox();
FlowDocument flowDoc = new FlowDocument();
Paragraph myPara = new Paragraph();
Run myRun = new Run(content);
myPara.Inlines.Add(myRun);
flowDoc.Blocks.Add(myPara);
fieldLabel.Document = flowDoc;
我希望看到红色的值,但我看到了标记。
提前感谢任何输入。
答案 0 :(得分:1)
您无法像这样分配RTF文本。您需要将该文本放入流中,然后将该流传递给RichTextBox.Selection.Load()方法。 e.g。
MemoryStream stream = new MemoryStream(UTF8Encoding.Default.GetBytes(yourRTFText));
fieldLabel.Selection.Load(stream, DataFormats.Rtf);
答案 1 :(得分:0)
您必须申请段落
// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
// Add paragraphs to the FlowDocument.
myFlowDoc.Background = Brushes.LightBlue;
myFlowDoc.Foreground = Brushes.DarkRed;
myFlowDoc.Typography.Capitals = FontCapitals.SmallCaps;
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
Paragraph p = new Paragraph(new Run("Paragraph 2"));
p.Foreground = Brushes.Black;
myFlowDoc.Blocks.Add(p);
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
RichTextBox myRichTextBox = new RichTextBox();
// Add initial content to the RichTextBox.
myRichTextBox.Document = myFlowDoc;