Click here to Skip to main content
15,792,384 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey guys

I need to append text to a WPF RichTextBox, but specific colors.

After much googling i put together the following method

private void AppendRtfText(string Text, Brush Color)<br />{<br />    TextRange range = new TextRange(txtChat.Document.ContentEnd, txtChat.Document.ContentEnd);<br />    range.Text = Text;<br />    range.ApplyPropertyValue(TextElement.ForegroundProperty, Color);<br />}

Now this works just fine, but I need to be able to specify RGB values.

The TextElement.ForegroundProperty only accepts a Brush and as far as I can see I cant specify RGB for a brush like i would with the Color class.

Any ideas? Cause I'm running out


1 solution

Taken from[^]

<br />SolidColorBrush mySolidColorBrush = new SolidColorBrush();<br /><br />// Describes the brush's color using RGB values. <br />// Each value has a range of 0-255.<br />mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);<br />

hth, Philipp

Share this answer

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900