我要做的是在布局网格中管理文本块和组合框。需要调整组合框的大小以适合它的选择(宽度=自动)。如果需要,文本块需要用椭圆修剪,或者当没有修剪时,列应该停止扩展超过文本块的宽度 - 而白色空间应该增长到组合的右侧。所以,我正在寻找Width =“*”的混合,同时修剪文本块,并且当没有修剪时Width =“Auto”。如果MaxWidth支持“自动”,那将是很好的。
我不想设置textblock列的特定MaxWidth,因为它会随全局化值而变化。如果只有xaml解决方案,我也更喜欢它,但如果没有,哦,好吧。此外,网格可能不是正确的容器。
这是一个例子。左列的宽度=“*”,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);
}
}
答案 0 :(得分:1)
您要么显式设置ColumnDefinition的MaxWidth,要么创建您自己的逻辑,以便在TextBox / ComboBox宽度更新时设置MaxWidth。您可以使用附加属性实现此目的,并将其附加到Grid.ColumnDefinitions。只要您知道如何使用它,WPF就是强大的,您可以随心所欲地做任何事情。
如果您不介意,可以提供一份现有代码示例。