Change color in part of text in textblock
Asked Answered
I

3

6

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!

Inconsolable answered 13/11, 2014 at 10:52 Comment(0)
I
9

That working

                        textblock1.Inlines.Clear();
                        textblock1.Inlines.Add(new Run(string1));
                        textblock1.Inlines.Add(new Run(string2) { Foreground = Brushes.Blue });
Inconsolable answered 13/11, 2014 at 12:37 Comment(0)
D
6

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>
Duckboard answered 13/11, 2014 at 11:20 Comment(2)
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 :/Inconsolable
please refer to this answers:#5442567Duckboard
P
0

You can use "Run" Ex : "Run Name="Value_from_codebehind" />"

In the code behind attach the value

Pasteur answered 27/12, 2018 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.