簡體   English   中英

如何在RichTextBox上更改部分文字顏色

[英]How to change part of text color at RichTextBox

美好的一天!

我嘗試將部分文字更改為紅色。

因此,我嘗試使用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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM