Change Color and Font for Some Part of Text in Wpf C#

Change color and font for some part of text in WPF C#

If you just want to do some quick coloring, the simplest solution may be to use the end of the RTB content as a Range and apply formatting to it. For example:

TextRange rangeOfText1 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
rangeOfText1.Text = "Text1 ";
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

TextRange rangeOfWord = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
rangeOfWord.Text = "word ";
rangeOfWord.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
rangeOfWord.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular);

TextRange rangeOfText2 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
rangeOfText2.Text = "Text2 ";
rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

If you are looking for a more advanced solution, I suggest reading this Microsoft Doc about Flow Document, as it gives you a great flexibility in formatting your text.

Change color for some part of text in WPF C#

Use System.Windows.Media.Brushes insted of System.Drawing.Brushes

C# WPF Textblock different font color per line

You can make use of Run.

Here is the sample of how to use the Run

Run run = new Run(smallPotion.Name + "(" + smallPotion.AffectValue + ")\r\n");
run.Foreground = Brushes.Green;
tbPotionInfo.Inlines.Add(run);

run = new Run(mediumPotion.Name + "(" + mediumPotion.AffectValue + ")\r\n");
run.Foreground = Brushes.Blue;
tbPotionInfo.Inlines.Add(run);
...

Haven't verified but I hope it will help you.

How to change font color from C# in WPF

An else condition is missing from your if statement in order to achieve what you need.

You can do it 1 of 2 ways:

if (nameDay.Text.Equals("Sunday"))
{
daytxt.Foreground = Brushes.Red;
}
else
{
daytxt.Foreground = Brushes.Black;
}

Or

daytxt.Foreground = nameDay.Text.Equals("Sunday") ? Brushes.Red : Brushes.Black;

How to change part of text color at RichTextBox

Starting with your example:

TextRange rangeOfText2 = new TextRange(tbScriptCode.Document.ContentEnd,
tbScriptCode.Document.ContentEnd);

rangeOfText2.Text = "RED !";

rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty,Brushes.Red);
rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

works for me.

Ok, i get TextRange, but how to append it to RichTextBox?

It has already been added with

new TextRange(tbScriptCode.Document.ContentEnd, tbScriptCode.Document.ContentEnd);

What you also can do (I used this myself lately):

var fd = new FlowDocument();

Paragraph paragraph = new Paragraph();

paragraph.Inlines.Add(new Run("normal text and this is in "));
paragraph.Inlines.Add(new Run("red") { Foreground = Brushes.Red });
paragraph.Inlines.Add(new Run(" and this is blue.") { Foreground = Brushes.Blue });

fd.Blocks.Add(paragraph);

tbScriptCode.Document = fd;

c# wpf change font color with color picker

Foreground is actually of brush, not limited to a solid color. If you want just one plain color as foreground, construct a SolidColorBrush with the chosen color.

// suppose your color picker is named yourColorPicker
var color = yourColorPicker.SelectedColor;
if (color.HasValue) // any color selected
lbText.Foreground = new SolidColorBrush(color.Value);
else // no color selected
lbText.ClearValue(Control.ForegroundProperty);

Check out more brushes for WPF at https://learn.microsoft.com/dotnet/framework/wpf/graphics-multimedia/wpf-brushes-overview



Related Topics



Leave a reply



Submit