Reputation: 132
How do I change the color of a word from a label.Content
? I mean if I have for example the following label content:
Hello cruel world !
How do I change only the color of the word cruel
?
Note: I do want to do this by code, TextRange
doesn't seem to work for my problem.
Upvotes: 0
Views: 699
Reputation: 8937
If you use Label to represent the text, you should use TextBlock instead. Then you can do like this:
TextBlock tb = new TextBlock();
tb.Inlines.Add(new Run("Hello"));
tb.Inlines.Add(new Run("cruel") { Foreground = Brushes.Tomato });
tb.Inlines.Add(new Run("world !"));
Label is a content container control, it means that it is used to show not only text, but something else (images, panels, texts). When you want to show the text only, you should use TextBlock in WPF. This allows you to work with it, as you describe earlier.
If the Label is required anyway, add a container (stackpanel for example) and add a TextBlock into it.
Upvotes: 5