如何保存在UWP应用TextBlock的白色空间 [英] How to preserve white-spaces of TextBlock in UWP apps
问题描述
如果您简单的设置Text属性的值在一个TextBlock为示例(请注意,有3空格在结束此字符串的),什么TextBlock的表演在UI仅仅是示例
If you simply set the value of Text property in a TextBlock as "Example " (Note that there 3 whitespaces at the end of this string),what TextBlock shows in UI is just "Example".
和搜索互联网上的解决方案后,我发现有是解决这个问题的方法
And after searching for solutions on the Internet, I found that there is a way to solve this issue:
<Border BorderThickness="1"
BorderBrush="#FFFF0202"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock x:Name="t1">
<Run Text="Example   "/>
</TextBlock>
</Border>
上面的代码显示了使用TextBlock的内联地产和&安培;# 160;
在运行的文本正确显示空白。
The above code shows the use of Inline Property of TextBlock and  
in Run's Text displays the whitespace correctly.
不过,即时我来说,我需要设置TextBlock的Text属性中的代码隐藏(或通过数据绑定),上面没有按招'T工作,它显示的 例&放大器;#160;&放大器;#160;&放大器;#160;
在UI
However, im my case I need to set the Text property of TextBlock in Code-behind(or via DataBinding), the trick above doesn't work and it shows Example   
in UI.
我试图设置数据绑定,运行的Text属性的,我认为能够正确显示转义字符的值,但是运行的Text属性不是依赖属性,所以我有没有更好的办法来解决这一点。
I tried to set the value of Run's Text property by data binding, which I think can displays the escape character correctly, but Run's Text property is NOT a dependency property so I have no better way to solve this.
(不过我认为TextBlock的使用padding属性也是一招,做到这一点,它应该工作。但是有没有更好的办法呢?)
(However I think use padding property of TextBlock is also a trick to do this, and it should work. But there is any better way to do ?)
推荐答案
首先, Run.Text
的确实的支持数据绑定。
First, Run.Text
does support data binding.
之所以&放大器;#160;
不中的数据绑定正确打印是因为它是使用XML逃生。字符
The reason that  
doesn't print correctly inside data binding is because it's using XML escape characters.
尝试使用(char)的160
代替 -
public string TestString { get; set; } = "Example" + (char)160 + (char)160 + (char)160;
<TextBlock>
<Run Text="{x:Bind TestString}" />
</TextBlock>
这篇关于如何保存在UWP应用TextBlock的白色空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!