Problem:
I have WPF TextBlock with dynamically changed text. This text contains words that are color names.
For example:
MyTextBlock.Text = "This is some dynamic text that contains red, green and yellow words inside it.";
Result should be rendered so all "red", "green" and "yellow" words are formatted with their color.
Note: Text is dynamically changed so static formatting is not an option.)
You can take benefit from
Inline
collection property insideTextBlock
to customize the content and set it dynamically..You would do something like this in code behind..
Where
UpdateTextBoxWithColoredText
isSample output
Note: punctuations are not handled in the solution above, also linebreaks (you can do
textblock.Inlines.Add(new LineBreak());
orstringBuilder.AppendLine();
), this is sort of string processing that you can do it in your behalf :)