/ / richTextBoxのテキストの色付け部分 - c#、wpf

richTextBoxのテキストの色付け部分 - c#、wpf

だから私は現在、小さなIRCボットを書いていますTwitch、そして私はWPFでそれをやっています。私はrichTextBoxに追加するテキスト行のユーザー名だけを色付けしたいと思います。私はシンプルなフォアグラウンドの色づけで試しましたが、毎回すべてを色づけしています。

私の現在のコード:

if (e.ChatMessage.ColorHex.StartsWith("#"))
{
richTextBox.Foreground = ChatUtils.convertHexToBrush(e.ChatMessage.ColorHex);
}

richTextBox.AppendText(String.Format("[{0}] <{1}>: {2}",
DateTime.Now.ToString("HH:mm:ss"),
e.ChatMessage.DisplayName, e.ChatMessage.Message) + "n");

richTextBox.ScrollToEnd();

ですから、私はどのようにパラメータ{1}だけを色付けしますか? e.ChatMessage.DisplayName

回答:

回答№1は1

このようにしてみてください。

TextRange tr = new TextRange(rtb.Document.ContentEnd,­ rtb.Document.ContentEnd);
tr.Text = e.ChatMessage.DisplayName;
tr.ApplyPropertyValue(TextElement.­ForegroundProperty, Brushes.Red);

これが役立つかどうかを見てください。