我要做的是在布局网格中管理文本块和组合框。组合框需要调整大小以适合其选择(宽度=自动)。如有必要,文本块需要用椭圆修剪,或者当它没有被修剪时,列应该停止扩展超过文本块的宽度 - 而空白应该增长到组合的右侧。所以,我有点想在修剪文本块时寻找 Width="*" 和未修剪时 Width="Auto" 的混合。如果 MaxWidth 支持“自动”那就太好了。
我不想设置 textblock 列的特定 MaxWidth ,因为它会随着全球化值而改变。如果有一个仅限 xaml 的解决方案,我也更喜欢它,但如果没有,哦,好吧。此外,也许网格不是正确的容器。
这是一个例子。左列有 Width="*" 和 MaxWidth="150"。这可以正常工作,但是像这样指定 MaxWidth 不适用于文本块中的全球化文本。我需要一些更有活力的东西。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MaxWidth="150"/>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left" Text="Input control one" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" Margin="2"/>
<TextBlock HorizontalAlignment="Left" Text="Input control two" VerticalAlignment="Center" Grid.Row="1" TextTrimming="CharacterEllipsis" Margin="2"/>
<TextBlock HorizontalAlignment="Left" Text="Input control number three" VerticalAlignment="Center" Grid.Row="2" TextTrimming="CharacterEllipsis" Margin="2"/>
<ComboBox HorizontalAlignment="Left" d:LayoutOverrides="Height" VerticalAlignment="Center" Grid.Column="1" Margin="2">
<ComboBoxItem Content="Item One" IsSelected="True"/>
</ComboBox>
<ComboBox HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" Margin="2">
<ComboBoxItem Content="Item One" />
<ComboBoxItem Content="Item Number Two" IsSelected="True"/>
</ComboBox>
</Grid>
解决方案...
感谢 Leo 让我找到解决方案。我只需要弄清楚如何从附加属性计算 MaxWidth。关键是要获得列中所有元素的最大期望宽度。还需要使用无穷大重新测量元素以获得所需的完整宽度。否则,您可以获得修剪后的所需宽度。
/// <summary>
/// AutoMaxWidth Dependency Property allows a ColumnDefinition to automatically default
/// it's MaxWidth to the correct width on the Load event of the ColumnDefinition.
/// </summary>
public static readonly DependencyProperty AutoMaxWidthAttachedProperty =
DependencyProperty.RegisterAttached ("AutoMaxWidth",
typeof (bool),
typeof (ColumnBehavior),
new UIPropertyMetadata (false, OnAutoMaxWidthAttachedPropertyChanged));
/// <summary>
/// Called when the AutoMaxWidth property has changed values
/// </summary>
/// <param name="o">The object this behavior is attached to.</param>
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
static void OnAutoMaxWidthAttachedPropertyChanged (DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ColumnDefinition col = (o as ColumnDefinition);
if (col == null) return;
if ((bool)e.NewValue)
col.Loaded += col_Loaded;
else
col.Loaded -= col_Loaded;
}
static void col_Loaded (object sender, RoutedEventArgs e)
{
ColumnDefinition col = sender as ColumnDefinition;
if (col == null) return;
Grid g = col.Parent as Grid;
if (g == null) return;
int index = g.ColumnDefinitions.IndexOf (col);
foreach (UIElement el in g.Children)
if (Grid.GetColumn (el) == index)
{
el.Measure (new Size (Double.PositiveInfinity, el.DesiredSize.Height));
if (col.MaxWidth == Double.PositiveInfinity)
col.MaxWidth = el.DesiredSize.Width;
else
col.MaxWidth = Math.Max (col.MaxWidth, el.DesiredSize.Width);
}
}