WPF TextBlock Padding 正在剪切文本

发布于 2024-08-04 22:08:05 字数 1458 浏览 7 评论 0原文

我在 Grid 中有一个 TextBlock,其 Padding 属性设置为 5。有时最后一个字符会被截断,具体取决于 的字符串code>Text 属性设置为。

<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 也可以使字符串显示得很好。

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>

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷雾森÷林ヴ 2024-08-11 22:08:05

您所描述的显然是 WPF 中的布局错误(可能在 TextBlock 中)。最后一个字母是否被换行(和切断)似乎取决于字符串的实际宽度以及最后一个字母相对于填充大小的大小。

我建议您报告该错误这里

要解决此问题,您可以使用以下代码(只需在文本块周围放置边框并在那里设置填充):

<Border Padding="5" Grid.Row="0" Grid.Column="1">
    <TextBlock  HorizontalAlignment="Left"
        Text="0x0F" TextWrapping="Wrap"
        VerticalAlignment="Top" />
</Border>

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.

I suggest you report the bug here.

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>
李不 2024-08-11 22:08:05

使包含文本块自动调整大小的网格列像这样

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <TextBlock Grid.COlumn="0" Text="{Binding Path=SomeViewModelProperty}" />
</Grid>

Make the column of the grid that contains textblock auto size like this

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <TextBlock Grid.COlumn="0" Text="{Binding Path=SomeViewModelProperty}" />
</Grid>
帅气称霸 2024-08-11 22:08:05

如果将 TextBlock 的高度设置为 100,F 是否会被换行?

If you set the height on the TextBlock to 100, does the F then get wrapped?

绮烟 2024-08-11 22:08:05

只需增加 ComboBoxItem 的高度,就可以解决问题。

Just increase the Height of the ComboBoxItem , it should solve the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文