Why does TextBlock trims ending spaces from the text?

后端 未结 5 2080
粉色の甜心
粉色の甜心 2021-02-19 01:18

Here is my TextBlocks


    

        
相关标签:
5条回答
  • 2021-02-19 01:25

    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.

    0 讨论(0)
  • 2021-02-19 01:28

    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.

    0 讨论(0)
  • 2021-02-19 01:30

    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.

    0 讨论(0)
  • 2021-02-19 01:32

    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>
    
    0 讨论(0)
  • 2021-02-19 01:36

    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.

    0 讨论(0)
提交回复
热议问题