Textblock.TextTrimming 在网格内不起作用

发布于 2024-10-06 16:13:51 字数 1563 浏览 4 评论 0原文

我的布局有一个 3 列网格,每个网格的宽度设置为 Width="*"。对于中间(第二个)网格,我有另一个 3 列网格,每个网格都包含自己的文本块,并且列网格宽度再次设置为 Width="*"

调整窗口大小时,网格会按预期调整大小,但是如果文本超出网格边界,则不会修剪第三个文本块。我使用 TextTrimming="WordEllipsis"TextWrapping="Wrap" 设置了文本框,并且由于某种原因未强制执行这些属性。

这是我的一些代码:

布局网格:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="150" MaxWidth="300" Width="1*" />
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition MinWidth="150" MaxWidth="500" Width="1*" />
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="2*"/>
    </Grid.ColumnDefinitions>
</Grid>

第二列代码:

<Grid Grid.Column="2" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="5" Width="Auto">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <TextBlock Text="{Binding Path=FeedItems.Count}" Foreground="White" FontSize="18" Width="Auto" FontWeight="SemiBold" />
    <TextBlock Text=" items from " Foreground="White" FontSize="18" Width="Auto" Grid.Column="1" />
    <TextBlock Text="{Binding Path=Name}" Foreground="White" FontSize="18" Grid.Column="2" TextTrimming="CharacterEllipsis" HorizontalAlignment="Left" Width="Auto" TextWrapping="NoWrap" ClipToBounds="True" />
</Grid>

I have a 3 column grid for my layout with each of it width set to Width="*". For the middle (2nd) grid, I have another 3 column grid each containing it own textblock, and again the column grids width are set to Width="*".

When the Window is resized, the grids are resized as expected, however the 3rd textblock isn't getting trimmed if the text goes outside the boundary of the grid. I have textbox set with TextTrimming="WordEllipsis" and TextWrapping="Wrap", and the properties are not being enforced for some reason.

Here is some of the code I have:

Layout grid:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="150" MaxWidth="300" Width="1*" />
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition MinWidth="150" MaxWidth="500" Width="1*" />
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="2*"/>
    </Grid.ColumnDefinitions>
</Grid>

2nd column code:

<Grid Grid.Column="2" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="5" Width="Auto">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <TextBlock Text="{Binding Path=FeedItems.Count}" Foreground="White" FontSize="18" Width="Auto" FontWeight="SemiBold" />
    <TextBlock Text=" items from " Foreground="White" FontSize="18" Width="Auto" Grid.Column="1" />
    <TextBlock Text="{Binding Path=Name}" Foreground="White" FontSize="18" Grid.Column="2" TextTrimming="CharacterEllipsis" HorizontalAlignment="Left" Width="Auto" TextWrapping="NoWrap" ClipToBounds="True" />
</Grid>

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

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

发布评论

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

评论(1

寄人书 2024-10-13 16:13:51

为了使其工作,您需要第二个网格中的最后一列具有 * 大小,否则它将告诉 TextBlock 它具有所需的空间,即使它没有't。自动调整大小的列不会将内容限制在网格的范围内。但是,如果使用单个 TextBlock 和多个 Run 执行此操作,可能会获得更好的结果:

<TextBlock FontSize="18" TextTrimming="CharacterEllipsis">
    <Run Text="{Binding Path=FeedItems.Count}" FontWeight="SemiBold" />
    <Run Text=" items from " />
    <Run Text="{Binding Path=Name}" />
</TextBlock>

请注意,从 .NET 4.0 开始,您只能绑定 Run.Text。如果您使用的是旧版本的框架,则必须创建自己的 BindableRun,这非常简单 此处

In order for this to work, you would need the last column in the second grid to have a * size, otherwise it will tell the TextBlock that it has as much space as it wants, even if it doesn't. Auto sized columns won't restrict the content to the bounds of a grid. However, you would probably get better results if you did this with a single TextBlock, and multiple Runs:

<TextBlock FontSize="18" TextTrimming="CharacterEllipsis">
    <Run Text="{Binding Path=FeedItems.Count}" FontWeight="SemiBold" />
    <Run Text=" items from " />
    <Run Text="{Binding Path=Name}" />
</TextBlock>

Note that you can only bind Run.Text as of .NET 4.0. If you're using an older version of the framework, you'll have to create your own BindableRun, which is pretty simple as seen here.

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