4

Is it possible to apply a background color to the inline text of a TextBlock instead of the entire rectangular bounds of the TextBlock? In other words, I would like to have this:

Screenshot

<TextBlock TextWrapping="Wrap">
    <!-- No such "Background" property -->
    <Span Background="Red">
        The quick brown fox jumps over the lazy dog
    </Span>
</TextBlock>

(Just like <span style="background-color: red"> in HTML)

Instead of this:

Screenshot

<Border Background="Red">
    <TextBlock TextWrapping="Wrap">
        The quick brown fox jumps over the lazy dog
    </TextBlock>
</Border>

Is this even supported? I've been looking through the documentation for the TextBlock and RichTextBlock classes but I can't see any way of setting the background of the inline text. I think WPF supports this.


EDIT

I should mention, I need this to work with automatically wrapped text. I don't know how many lines there will be, or else I could just manually use a Border for each line of text.

Decade Moon
  • 32,968
  • 8
  • 81
  • 101

1 Answers1

3

It seems that this is not currently supported in UWP apps.

WPF has a Background property for Run elements, which can be used inside TextBlock, but UWP's Run does not support this.

The only way I can think of to implement this would be using HTML and a WebView, or somehow using Win2D rendering, both of which are quite cumbersome solutions...

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91