WPF RichTextBox appending coloured text

Aks picture Aks · Apr 1, 2011 · Viewed 26.8k times · Source

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?

Answer

Kishore Kumar picture Kishore Kumar · Apr 1, 2011

Just try this:

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