WPF TextBlock.TextTrimming 不适用于自动调整大小的 ColumnDefinition

发布于 2024-10-03 21:00:13 字数 2547 浏览 4 评论 0原文

我使用 WPF 网格作为窗口的布局。它有两列和任意数量的行。第一列专门用于标签,第二列用于用户输入字段(例如文本框、组合框等)。我的要求是:

  1. 第一列的最小宽度必须为 50,最大宽度为 180。
  2. 第一列的大小必须适合其内容,除非它不符合第一个要求。
  3. 第二列必须占据所有剩余空间。

我尝试了下面的 XAML:

<Grid>
    <Grid.ColumnDefinitions>
       <ColumnDefinition Width="Auto" MinWidth="50" MaxWidth="180" />
       <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
       <RowDefinition Height="Auto" />
       <RowDefinition Height="Auto" />
       <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TextBlock Text="First Name" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Column="1" Text="{Binding FirstName}" />

    <TextBlock Grid.Row="1" Text="Family Name" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding FamilyName}" />

    <TextBlock Grid.Row="2" Text="Label That Won't Fit in 180 units" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Text}" />
</Grid>

我希望第三行标签“无法容纳 180 个单位的标签”将被截断为“赢得的标签...”之类的内容。相反,它只是被剪辑为“Label That Won't”,其中一半的“t”丢失了。

我尝试了在网上某处找到的不同方法。

<Grid>
    <Grid.ColumnDefinitions>
       <ColumnDefinition x:Name="LabelColumn" Width="Auto" MinWidth="50" MaxWidth="180" />
       <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
       <RowDefinition Height="Auto" />
       <RowDefinition Height="Auto" />
       <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TextBlock Width="{Binding ActualWidth, ElementName=LabelColumn}" Text="First Name" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Column="1" Text="{Binding FirstName}" />

    <TextBlock Grid.Row="1" Width="{Binding ActualWidth, ElementName=LabelColumn}" Text="Family Name" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding FamilyName}" />

    <TextBlock Grid.Row="2" Width="{Binding ActualWidth, ElementName=LabelColumn}" Text="Label That Won't Fit in 180 units" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Text}" />
</Grid>

它最终在 Expression Blend 上工作(有时......),但在运行应用程序时却不起作用。运行时,所有标签完全消失。我通过监视窗口看到所有 TextBlock 的实际宽度均为 0,而 LabelColumn.ActualWidth 等于 80。

还有哪些其他选项?

I'm using a WPF Grid as the layout of my window. It has two columns and any number of rows. The first column is used specifically for labels and the second column is used for user-input fields (e.g. TextBox, ComboBox, etc.). My requirements are:

  1. The first column must have a minimum width of 50 and maximum width of 180.
  2. The first column must size to its contents except when it defies the first requirement.
  3. The second column must occupy all the remaining space.

I tried the XAML below:

<Grid>
    <Grid.ColumnDefinitions>
       <ColumnDefinition Width="Auto" MinWidth="50" MaxWidth="180" />
       <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
       <RowDefinition Height="Auto" />
       <RowDefinition Height="Auto" />
       <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TextBlock Text="First Name" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Column="1" Text="{Binding FirstName}" />

    <TextBlock Grid.Row="1" Text="Family Name" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding FamilyName}" />

    <TextBlock Grid.Row="2" Text="Label That Won't Fit in 180 units" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Text}" />
</Grid>

I was hoping that the third row label, "Label That Won't Fit in 180 units", will be truncated into something like "Label That Won...". Instead, it just got clipped to "Label That Won't" with half of the "t" missing.

I tried a different approach i found on the web somewhere.

<Grid>
    <Grid.ColumnDefinitions>
       <ColumnDefinition x:Name="LabelColumn" Width="Auto" MinWidth="50" MaxWidth="180" />
       <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
       <RowDefinition Height="Auto" />
       <RowDefinition Height="Auto" />
       <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TextBlock Width="{Binding ActualWidth, ElementName=LabelColumn}" Text="First Name" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Column="1" Text="{Binding FirstName}" />

    <TextBlock Grid.Row="1" Width="{Binding ActualWidth, ElementName=LabelColumn}" Text="Family Name" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding FamilyName}" />

    <TextBlock Grid.Row="2" Width="{Binding ActualWidth, ElementName=LabelColumn}" Text="Label That Won't Fit in 180 units" TextTrimming="CharacterEllipsis" />
    <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Text}" />
</Grid>

It ended up working on Expression Blend (Sometimes...) but not when running the application. When running, all the labels disappeared completely. I saw through the watch window that all the TextBlocks have an Actual Width of 0 while the LabelColumn.ActualWidth is equal to 80.

What other options are there?

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

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

发布评论

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

评论(1

赤濁 2024-10-10 21:00:13

您需要在 TextBlock 上设置 MinWidthMaxWidth,因为在列定义上设置 MinWidthMaxWidth 是在渲染上并不总是受到尊重。
在绑定表达式中使用 ActualWidth 可能会出现问题,因为 ActualWidth 是在多个渲染过程中设置的,并且可能会产生不可预测的结果

You need to have the MinWidth and MaxWidth set on the TextBlock, as setting MinWidth and MaxWidth on column definitions is not always honored on rendering.
Using ActualWidth within binding expressions can be problematic as ActualWidth is set during multiple render passes and can give unpredictable results

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