Why does TextBlock trims ending spaces from the text?

Inder Kumar Rathore picture Inder Kumar Rathore · Jan 15, 2013 · Viewed 9.4k times · Source

Here is my TextBlocks

<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
    <TextBlock Text="6 or more characters, at least one letter and a number,   "  FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
    <TextBlock Text="no symbols"  FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
</StackPanel>

And here is the output (screen shot) enter image description here

Why TextBlock trims ending spaces? However it works fine when I give leading spaces.

Answer

Jim O&#39;Neil picture Jim O'Neil · Jan 15, 2013

It looks like xml:space="preserve" should do the trick (see Preserving Whitespace in XAML) but that doesn't seem to be working in a Windows Store app (it does in WPF).

If you use the non-breaking space character &#160; it does work

 <TextBlock Text="6 or more characters, at least one letter and a number,&#160;&#160;&#160;&#160;&#160;&#160;&#160;"  ....

I suppose you could try building a converter on the Text property to check for trailing spaces and replace with non-breaking spaces - presuming the truncation that's happening doesn't occur too early.