Search code examples
c#wpfcolorsrichtextbox

WPF RichTextBox appending coloured text


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?


Solution

  • Just try this:

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