C# 需要网格列的大小达到宽度=“;“自动”;停下来
我想做的是在布局网格中管理文本块和组合框。组合框需要调整大小以适合其选择(宽度=自动)。如有必要,textblock需要用椭圆进行修剪,或者当它未被修剪时,列应停止扩展超过textblocks宽度-相反,空格应增长到组合框的右侧。因此,我在寻找文本块被修剪时的Width=“*”和未被修剪时的Width=“Auto”的混合。如果MaxWidth支持“自动”,那就太好了 我不想设置textblock列的特定MaxWidth,因为这将随全球化值而改变。我也希望有一个只有xaml的解决方案,但如果没有,哦,好吧。此外,网格可能不是用于此目的的正确容器 这里有一个例子。左列有Width=“*”和MaxWidth=“150”。这是正确的,但这样指定MaxWidth将无法处理文本块中的全球化文本。我需要更有活力的东西C# 需要网格列的大小达到宽度=“;“自动”;停下来,c#,wpf,xaml,C#,Wpf,Xaml,我想做的是在布局网格中管理文本块和组合框。组合框需要调整大小以适合其选择(宽度=自动)。如有必要,textblock需要用椭圆进行修剪,或者当它未被修剪时,列应停止扩展超过textblocks宽度-相反,空格应增长到组合框的右侧。因此,我在寻找文本块被修剪时的Width=“*”和未被修剪时的Width=“Auto”的混合。如果MaxWidth支持“自动”,那就太好了 我不想设置textblock列的特定MaxWidth,因为这将随全球化值而改变。我也希望有一个只有xaml的解决方案,但如果没有,
<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>
解决方案
谢谢利奥帮我找到了解决方案。我只需要弄清楚如何从附加属性计算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);
}
}
//
///AutoMaxWidth依赖项属性允许列定义自动默认
///它将MaxWidth设置为ColumnDefinition加载事件的正确宽度。
///
公共静态只读从属属性AutoMaxWidthAttachedProperty=
DependencyProperty.RegisterAttached(“AutoMaxWidth”,
类型(bool),
类型(行为),
新的UIPropertyMetadata(假,OnAutoMaxWidthAttachedPropertyChanged);
///
///当AutoMaxWidth属性的值更改时调用
///
///此行为附加到的对象。
///包含事件数据的实例。
AutoMaxWidthAttachedPropertyChanged上的静态无效(DependencyObject o、DependencyPropertyChangedEventArgs e)
{
ColumnDefinition col=(o作为ColumnDefinition);
if(col==null)返回;
if((bool)e.NewValue)
列加载+=列加载;
其他的
col.Loaded-=col_Loaded;
}
已加载静态无效列(对象发送器、路由目标)
{
ColumnDefinition col=发送方作为ColumnDefinition;
if(col==null)返回;
网格g=作为网格的父列;
如果(g==null)返回;
int index=g.ColumnDefinitions.IndexOf(col);
foreach(g.Children中的UIEL元素)
if(Grid.GetColumn(el)=索引)
{
el.测量(新尺寸(双正不确定度,el.所需尺寸.高度));
if(col.MaxWidth==Double.PositiveInfinity)
col.MaxWidth=el.DesiredSize.Width;
其他的
col.MaxWidth=Math.Max(col.MaxWidth,el.DesiredSize.Width);
}
}
您可以显式设置列定义的最大宽度,也可以创建自己的逻辑,以便在文本框/组合框宽度更新时设置最大宽度。您可以使用附加的属性实现这一点,并将其附加到Grid.ColumnDefinitions。WPF功能强大,只要您知道如何使用它,就可以做任何您想做的事情
如果您不介意的话,可以提供您现在拥有的示例代码。您可以显式设置列定义的最大宽度,或者创建自己的逻辑,在文本框/组合框宽度更新时设置最大宽度。您可以使用附加的属性实现这一点,并将其附加到Grid.ColumnDefinitions。WPF功能强大,只要您知道如何使用它,就可以做任何您想做的事情
如果你不介意的话,你能提供一个你现在拥有的示例代码吗。我已经在我原来的帖子中添加了一个示例。列上的MaxWidth确实有效,但对于全球化文本来说,它的动态性不够。对于这一点,行为是否比附加属性更合适?我仍然是一个WPF新手,仍然在为我遇到的每一个障碍做大量的研究。令人沮丧和有趣。我一直在处理一个附加属性/行为,它将计算列的最大宽度。这听起来像是解决办法。但是,我很难计算MaxWidth值以及确定要连接到哪个事件(Grid.LayoutUpdated看起来很有希望,但发送方总是空的)。谢谢。我在上面的文章中为我的附加属性添加了一些代码。列上的MaxWidth确实有效,但对于全球化文本来说,它的动态性不够。对于这一点,行为是否比附加属性更合适?我仍然是一个WPF新手,仍然在为我遇到的每一个障碍做大量的研究。令人沮丧和有趣。我一直在处理一个附加属性/行为,它将计算列的最大宽度。这听起来像是解决办法。但是,我很难计算MaxWidth值以及确定要连接到哪个事件(Grid.LayoutUpdated看起来很有希望,但发送方总是空的)。谢谢。我让它工作,并添加了一些代码为我的附加财产在上述职位。