在 RichTextBox 中更改颜色
Change color in RichTextBox
我正在使用 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
类没有任何直接改变颜色的方法。
编辑
我不想在链接 post! 上使用所有笨拙的 SelectionStart
、SelectionEnd
内容作为 posted。
我的情况不同并且简单得多:posted from mm8 的解决方案解释了它并且非常优雅。
一行代码,就是!
请看答案!
您可以通过富文本框的前景 属性 get/set 文本颜色。作为下面的例子,我将富文本框的文本颜色更改为蓝色:
myRichTextBox.Foreground = Brushes.Blue;
编码愉快!
The Paragraph
or Run
classes doesn't have any direct method to change the color.
Run
class 继承自 TextElement
而这个 class 有一个 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 });
我正在使用 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
类没有任何直接改变颜色的方法。
编辑
我不想在链接 post! 上使用所有笨拙的 SelectionStart
、SelectionEnd
内容作为 posted。
我的情况不同并且简单得多:posted from mm8 的解决方案解释了它并且非常优雅。 一行代码,就是!
请看答案!
您可以通过富文本框的前景 属性 get/set 文本颜色。作为下面的例子,我将富文本框的文本颜色更改为蓝色:
myRichTextBox.Foreground = Brushes.Blue;
编码愉快!
The
Paragraph
orRun
classes doesn't have any direct method to change the color.
Run
class 继承自 TextElement
而这个 class 有一个 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 });