WPF TextBlock填充正在截断文本 [英] WPF TextBlock Padding is cutting off text
问题描述
我在Grid
中有一个TextBlock
,其Padding
属性设置为5.有时,最后一个字符被截断,具体取决于Text
属性设置为哪个字符串.
I have a TextBlock
in a Grid
with its Padding
attribute set to 5. Sometimes the last character is cut off, depending on what string the Text
property is set to.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SomeClass">
<ScrollViewer Padding="5" VerticalScrollBarVisibility="Auto">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label
Grid.Row="0" Grid.Column="0"
Content="SomeLabel"
HorizontalAlignment="Right"
HorizontalContentAlignment="Right"
VerticalAlignment="Center" />
<TextBlock
Grid.Row="0" Grid.Column="1"
HorizontalAlignment="Left"
Padding="5"
Text="0x0F"
TextWrapping="Wrap"
VerticalAlignment="Top" />
</Grid>
</ScrollViewer>
</UserControl>
当Text
设置为0x0F
时,F
不可见.将其设置为0xAB
时,字符串显示就很好.将Padding
设置为0也可以使字符串显示得很好.
When the Text
is set to 0x0F
the F
is not visible. When it is set to 0xAB
the string displays just fine. Setting the Padding
to 0 also makes the string display just fine.
推荐答案
您所描述的显然是WPF中的布局错误(可能在TextBlock中).最后一个字母是否被包裹(切掉)似乎取决于字符串的实际宽度以及相对于填充大小的最后一个字母的大小.
What you describe is obviously a layout bug in WPF (probably in the TextBlock). Whether or not the last letter is wrapped (and cut off) seems to depends on the actual width of the string and the size of the last letter in respect to the size of the padding.
我建议您报告错误此处.
要变通解决此问题,您可以使用以下代码(只需在文本块周围放置边框并在其中设置填充)即可:
To work around this issue you can use the following code (just put a border around you textblock and set the padding there instead):
<Border Padding="5" Grid.Row="0" Grid.Column="1">
<TextBlock HorizontalAlignment="Left"
Text="0x0F" TextWrapping="Wrap"
VerticalAlignment="Top" />
</Border>
这篇关于WPF TextBlock填充正在截断文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!