如何在RichTextBox上更改部分文字颜色 [英] How to change part of text color at RichTextBox
本文介绍了如何在RichTextBox上更改部分文字颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
美好的一天!
我尝试将部分文字更改为红色.
I try to change part of text to red color.
因此,我尝试使用TextBox,但是它不起作用.因此,我读到RichTextBox可以做到:
So, i try to use TextBox, but it not works. So, i read, that RichTextBox can do that:i use this question
但是我不知道如何添加彩色文字?
But i do not know how to append colored text?
TextRange rangeOfText1 = new TextRange(tbScriptCode.Document.ContentEnd, tbScriptCode.Document.ContentEnd);
rangeOfText1.Text = "Text1 ";
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
好吧,我得到了TextRange,但是如何将其附加到RichTextBox?
Ok, i get TextRange, but how to append it to RichTextBox?
您能告诉我如何将文本的某些部分变成红色吗?谢谢!
Can you tell me how to make some part of text to red color? Thank you!
推荐答案
从您的示例开始:
TextRange rangeOfText2 = new TextRange(tbScriptCode.Document.ContentEnd,
tbScriptCode.Document.ContentEnd);
rangeOfText2.Text = "RED !";
rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty,Brushes.Red);
rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
为我工作.
好吧,我得到了TextRange,但是如何将其附加到RichTextBox?
Ok, i get TextRange, but how to append it to RichTextBox?
它已经被添加了
new TextRange(tbScriptCode.Document.ContentEnd, tbScriptCode.Document.ContentEnd);
您还可以做些什么(我最近自己用了):
What you also can do (I used this myself lately):
var fd = new FlowDocument();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run("normal text and this is in "));
paragraph.Inlines.Add(new Run("red") { Foreground = Brushes.Red });
paragraph.Inlines.Add(new Run(" and this is blue.") { Foreground = Brushes.Blue });
fd.Blocks.Add(paragraph);
tbScriptCode.Document = fd;
这篇关于如何在RichTextBox上更改部分文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文