作者:mobiledu2502909533 | 来源:互联网 | 2022-11-30 15:29
我在MSDN中使用这个简单的例子来插入一行RichTextBox
.
FlowDocument myFlowDoc = new FlowDocument();
Run myRun = new Run("This is flow content and you can ");
Bold myBold = new Bold(new Run("edit me!"));
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(myBold);
myFlowDoc.Blocks.Add(myParagraph);
RichTextBox myRichTextBox = new RichTextBox();
myRichTextBox.Document = myFlowDoc;
我想在文本行中应用一种选择的颜色,但是怎么做呢?
本Paragraph
或Run
类没有任何直接的方法来改变颜色.
编辑
我不想使用所有的awkard SelectionStart
,SelectionEnd
链接帖子上张贴的东西!
我的情况不同而且更简单:mm8发布的解决方案解释了它并且非常优雅.一行代码就是这样!
请看答案!
1> mm8..:
本Paragraph
或Run
类没有任何直接的方法来改变颜色.
在Run
类继承自TextElement
这个类有一个Foreground
可以设置一个属性Brush
:
Run myRun = new Run("This is flow content and you can ") { Foreground = Brushes.Red };
Bold myBold = new Bold(new Run("edit me!") { Foreground = Brushes.Gray });