2

我正在使用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;

我想看到红色的值,但我看到的是标记。

提前感谢您的任何意见。

4

2 回答 2

1

您不能像那样分配 RTF 文本。您需要将该文本放入流中,然后将该流传递给 RichTextBox.Selection.Load() 方法。例如

MemoryStream stream = new MemoryStream(UTF8Encoding.Default.GetBytes(yourRTFText));
fieldLabel.Selection.Load(stream, DataFormats.Rtf);
于 2012-08-10T22:07:59.577 回答
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;
于 2012-08-10T23:13:13.363 回答