WPF RichTextBox appending coloured text

前端 未结 4 1804
谎友^
谎友^ 2021-02-04 01:38

I\'m using the RichTextBox.AppendText function to add a string to my RichTextBox. I\'d like to set this with a particular colour. How can I do this?

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 02:07

    Just try this:

    TextRange tr = new TextRange(rtb.Document.ContentEnd,­ rtb.Document.ContentEnd);
    tr.Text = "textToColorize";
    tr.ApplyPropertyValue(TextElement.­ForegroundProperty, Brushes.Red);
    

提交回复
热议问题