您好!
我试着把部分文字改成红色。
所以,我尝试使用TextBox,但它不起作用。所以,我读到,RichTextBox可以做到这一点:我使用这个问题
但是我不知道如何添加彩色文本?
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?
你能告诉我如何把文本的某些部分变成红色吗?非常感谢。
从您的示例开始:
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?
它已经添加了
new TextRange(tbScriptCode.Document.ContentEnd, tbScriptCode.Document.ContentEnd);
你也可以做什么(我最近自己也用过):
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;