Change color in part of text in textblock

12,360

Solution 1

That working

                        textblock1.Inlines.Clear();
                        textblock1.Inlines.Add(new Run(string1));
                        textblock1.Inlines.Add(new Run(string2) { Foreground = Brushes.Blue });

Solution 2

I Hope that will help you:

<TextBlock FontSize="16">
        <Run Foreground="Red">Your_Text_1</Run>
        <Run Foreground="Orange">Your_Text_2</Run>
        <Run Foreground="purple">Your_Text_3</Run>
</TextBlock>
Share:
12,360
Honza Kalina
Author by

Honza Kalina

Updated on June 21, 2022

Comments

  • Honza Kalina
    Honza Kalina almost 2 years

    I need change color in code behind for part of text string..

    My exaple:

    textblock1.Text = string1 + string2 + string3;
    

    String have dynamic values, and i want to string2 display after running the program in blue color and it must be defined in the code behind.

    Its possible? Thank you!

  • Honza Kalina
    Honza Kalina over 9 years
    thank you for reply, but i need it define in code behind because textblock dont show static text, but different numbers of variables with different values :/
  • Dotnetter
    Dotnetter over 9 years
    please refer to this answers:stackoverflow.com/questions/5442067/…