Если вы хотите, вы также можете сделать это методом расширения.
public static void AppendText(this RichTextBox box, string text, string color)
{
BrushConverter bc = new BrushConverter();
TextRange tr = new TextRange(box.Document.ContentEnd, box.Document.ContentEnd);
tr.Text = text;
try
{
tr.ApplyPropertyValue(TextElement.ForegroundProperty,
bc.ConvertFromString(color));
}
catch (FormatException) { }
}
Это сделает так, что вы можете просто сделать
myRichTextBox.AppendText("My text", "CornflowerBlue");
или в шестнадцатеричном виде, таком как
myRichTextBox.AppendText("My text", "0xffffff");
Если введенная вами цветовая строка недопустима, она просто печатает ее в цвете по умолчанию (черный).Надеюсь, это поможет!