16

Here are 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 does TextBlock trim ending spaces? However, it works fine when there are leading spaces.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184

5 Answers5

23

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.

Jim O'Neil
  • 23,344
  • 7
  • 42
  • 67
  • non-breaking space does the trick but `space` property is easy one to do :( – Inder Kumar Rathore Jan 15 '13 at 18:08
  • Update from WinRT 8.1. `xml:space` still does not work, and this solution does. – Stephen Hosking May 01 '14 at 01:58
  • 4
    This (using  ) does NOT currently seem to work on a Windows 10 Universal app. I had to set it simply as content between the opening and closing tags rather than setting the Text property to get it to get it to render correctly. Tried a number of other variants to no avail (\u0020, #nbsp, etc.) – matthewsheets Jun 25 '15 at 21:31
9

Solved with <Run /> in a <TextBlock />..

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

And word wrapping still works

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

I would easily use Jim's solution (#160;) if wrapping was not an issue.

In your mind please think about how HTML handles and preserves spaces. This is also how XAML handles and preserves spaces. You would think, of course, that inside a TextBlock it would be more literally handled, huh? Well, it is what it is. At least there's a solution.

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
  • Jerry why actually I was doing this is when my app is in normal mode the text should be **`6 or more characters, at least one letter and a number, no symbols`** and in snap view mode it should be **`6 or more characters, at least one letter and a number, no symbols`**. I have done this. But I want that these texts should come from `.resw` file. In other words how would I set text in **` `** just like `` – Inder Kumar Rathore Jan 16 '13 at 03:57
  • I have posted a **[question related to this also here](http://stackoverflow.com/q/14343376/468724)** – Inder Kumar Rathore Jan 16 '13 at 04:00
  • The same `` – Jerry Nixon Jan 16 '13 at 14:54
4

Try use xml:space="preserve":

<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
    <TextBlock xml:space="preserve" Text="6 or more characters, at least one letter and a number,   "  FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
    <TextBlock xml:space="preserve" Text="no symbols"  FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
</StackPanel>
Norbert Pisz
  • 3,392
  • 3
  • 27
  • 42
1

I've found a different solution! The \u+A0 works when you ALSO set the IsTextSelectionEnabled.

I don't know why this would be, and it was a total surprise (I added the field because I just discovered it while also working on my 'Why does my text get trimmed in Universal Apps?' problem).

Also U+205F (medium mathematical space) also works in conjunction with IsTextSelectionEnabled.

armatita
  • 12,825
  • 8
  • 48
  • 49
PESMITH_MSFT
  • 350
  • 1
  • 9
0

RichTextBlock seems to preserve both leading and trailing whitespace (in WP 8.1 WinRT):

<RichTextBlock>
 <RichTextBlock.Blocks>
  <Paragraph >
   <Paragraph.Inlines>
    <Run Text="trailing " /><Run Text="bbb" /><Run Text=" leading" />
   </Paragraph.Inlines>
  </Paragraph>
 </RichTextBlock.Blocks>
</RichTextBlock>

But it also seems to add an extra space between the runs in addition to the of the ones you specify.