WPF TextBlock.TextTrimming 不适用于自动调整大小的 ColumnDefinition
我使用 WPF 网格作为窗口的布局。它有两列和任意数量的行。第一列专门用于标签,第二列用于用户输入字段(例如文本框、组合框等)。我的要求是:
- 第一列的最小宽度必须为 50,最大宽度为 180。
- 第一列的大小必须适合其内容,除非它不符合第一个要求。
- 第二列必须占据所有剩余空间。
我尝试了下面的 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:
- The first column must have a minimum width of 50 and maximum width of 180.
- The first column must size to its contents except when it defies the first requirement.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 TextBlock 上设置
MinWidth
和MaxWidth
,因为在列定义上设置MinWidth
和MaxWidth
是在渲染上并不总是受到尊重。在绑定表达式中使用
ActualWidth
可能会出现问题,因为ActualWidth
是在多个渲染过程中设置的,并且可能会产生不可预测的结果You need to have the
MinWidth
andMaxWidth
set on the TextBlock, as settingMinWidth
andMaxWidth
on column definitions is not always honored on rendering.Using
ActualWidth
within binding expressions can be problematic asActualWidth
is set during multiple render passes and can give unpredictable results