简体   繁体   English

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

[英]WPF TextBlock.TextTrimming Not Working With Auto-sized ColumnDefinition

I'm using a WPF Grid as the layout of my window. 我正在使用WPF网格作为窗口的布局。 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 (eg TextBox, ComboBox, etc.). 第一列专门用于标签,第二列用于用户输入字段(例如TextBox,ComboBox等)。 My requirements are: 我的要求是:

  1. The first column must have a minimum width of 50 and maximum width of 180. 第一列的最小宽度必须为50,最大宽度必须为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: 我试过下面的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>

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...". 我希望第三行标签“不符合180个单位的标签”将被截断为“标签赢得......”之类的东西。 Instead, it just got clipped to "Label That Won't" with half of the "t" missing. 相反,它被剪切到“标签不会”,一半的“t”丢失。

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. 它最终处理Expression Blend(有时......),但在运行应用程序时却没有。 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. 我通过监视窗口看到所有TextBlocks的实际宽度为0,而LabelColumn.ActualWidth等于80。

What other options are there? 还有哪些其他选择?

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. 你需要有MinWidthMaxWidth上TextBlock的设置,如设置MinWidthMaxWidth上栏定义并不总是渲染荣幸。 Using ActualWidth within binding expressions can be problematic as ActualWidth is set during multiple render passes and can give unpredictable results 在绑定表达式中使用ActualWidth可能会有问题,因为在多个渲染过程中设置了ActualWidth并且可能会产生不可预测的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM