Why does TextBlock trims ending spaces from the text?
Asked Answered
C

5

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.

Coprophagous answered 15/1, 2013 at 12:21 Comment(0)
U
24

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.

Unwarranted answered 15/1, 2013 at 14:35 Comment(3)
non-breaking space does the trick but space property is easy one to do :(Coprophagous
Update from WinRT 8.1. xml:space still does not work, and this solution does.Pennant
This (using &#160;) 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.)Bogoch
M
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.

Mycobacterium answered 15/1, 2013 at 18:38 Comment(3)
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,&#10;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 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Text" Storyboard.TargetName="textBlock"> <DiscreteObjectKeyFrame KeyTime="0" Value="Text From resw file"/> just like <TextBlock x:Uid="txt"/>Coprophagous
I have posted a question related to this also hereCoprophagous
The same <Run Text="{Binding Whatever}" />Mycobacterium
T
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>
Timbuktu answered 15/1, 2013 at 14:25 Comment(1)
+1 for space attribute. But that doesn't working in store appsCoprophagous
M
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.

Malisamalison answered 14/5, 2016 at 3:36 Comment(1)
As of 2016-10-06, the IsTextSelectionEnabled trick doesn't work any more,Malisamalison
P
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.

Pomeroy answered 25/6, 2014 at 23:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.