Styling part of the text in TextBlock

I want to display some HTML text in TextBlock. I believe this is not possible. The reason I want to use HTML text is to apply font style, e.g. bold and specific color, to part of the displayed text.
Is there a way to apply a specific font styling for a part of the text shown in a TextBlock element? It’s not necessary to use TextBlock, but any object that supports such a feature is fine for me.


You are right – you cannot do that now with a single TextBlock. This is a commonly requested feature.

I suppose for simple single-line cases you could use multiple TextBlocks in a “Horizontal” Panel. Of course that won’t help when you want wrapping or multiple logical lines.

The text is multiline and the parts that need special font styles are marked by an XML tag. It would be cumbersome to predetermine that number of textblock elements that are required to show all lines.

It would be great if you could add HTML support in future release :)

Would you want something like what Java Swing supports – a very limited subset of inline HTML? (Unless they have extended its support for more general HTML recently; I’m no longer up-to-date on Swing.)

More to the point – exactly what styling options do you need?

A limited subset of inline HTML is fine as I want to show some HTML like “

This is Line 1
.” It would be useful if the styling can be specified at a global level so that we don’t have to repeat it on every line. So the resulting line would look like "
This is Line 1
I don’t expect to use any complex elements to show text.

At present I want to display text in DIV and SPAN elements with support for Color, Font-Size, Font-Family and Font-Weight. It would be great if the element can support all Font properties in HTML. The TextBlock, or any other element that you may support, will need to support multiple lines where a line may or may not be wrapped in a DIV element. The DIV element should behave like the HTML block element so that we don’t have to add any newline characters to the text.

OK, thanks for the input. We’ll see what we can do, although I cannot promise that it will be soon.

If you or anyone else has any additional details/requests about styled text, just add them here.

Seconding this. I’d like access to set the line-height of a text block. As it is, text blocks often look messy since the lines are rather close together.

There are two undocumented TextBlock properties that you can experiment with: spacingAbove and spacingBelow. The default values are zero.

Thanks, Walter! This is exactly what I needed :smile: