为什么 TextBlock 会修剪文本中的结尾空格? [英] Why does TextBlock trims ending spaces from the text?

查看:33
本文介绍了为什么 TextBlock 会修剪文本中的结尾空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的TextBlock:

<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):

为什么 TextBlock 会修剪结尾空格?但是,当有前导空格时它工作正常.

Why does TextBlock trim ending spaces? However, it works fine when there are leading spaces.

推荐答案

看起来 xml:space="preserve" 应该解决这个问题(见 在 XAML 中保留空白)但这似乎不适用于 Windows 应用商店应用程序(在 WPF 中确实如此).

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).

如果您使用不间断空格字符 &#160; 它确实有效

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;"  ....

我想您可以尝试在 Text 属性上构建一个转换器来检查尾随空格并替换为不间断空格 - 假设发生的截断不会太早发生.

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.

这篇关于为什么 TextBlock 会修剪文本中的结尾空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆