C# WPF组合框:多列宽度
我在WPF中有一个组合框,其中包含列表部分的多个列。只要我明确指定列的宽度,一切都正常。如果我为第一列的宽度指定“auto”,则网格结构将崩溃,并且每行的列彼此不对齐。我想要的是,第一列的宽度能够达到所需的宽度,以便修复最长行的文本,而另一列可以填充剩余的可用空间 我的XAML如下:C# WPF组合框:多列宽度,c#,wpf,combobox,C#,Wpf,Combobox,我在WPF中有一个组合框,其中包含列表部分的多个列。只要我明确指定列的宽度,一切都正常。如果我为第一列的宽度指定“auto”,则网格结构将崩溃,并且每行的列彼此不对齐。我想要的是,第一列的宽度能够达到所需的宽度,以便修复最长行的文本,而另一列可以填充剩余的可用空间 我的XAML如下: <ComboBox x:Name="CommandListComboBox" Margin="95,0.48,110,30" ItemsSource="{Binding}" HorizontalContent
<ComboBox x:Name="CommandListComboBox" Margin="95,0.48,110,30" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch" SelectionChanged="CommandListComboBox_OnSelectionChanged" IsEditable="True" IsReadOnly="True" DisplayMemberPath="Description">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid x:Name="gd" TextElement.Foreground="Black" Width="{Binding Path=ActualWidth,ElementName=CommandListComboBox}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="225"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="5" Grid.Column="0" Text="{Binding Description}" TextWrapping="Wrap"/>
<TextBlock Margin="5" Grid.Column="1" Text="{Binding DeviceListText}" TextWrapping="Wrap"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ListBoxItem.IsSelected" Value="True">
<Setter TargetName="gd" Property="Background" Value="Gray"/>
<Setter TargetName="gd" Property="TextElement.Foreground" Value="White"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="gd" Property="Background" Value="Blue"/>
<Setter TargetName="gd" Property="TextElement.Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
谢谢。为列创建一个
SharedSizeGroup
(替换为
)
工作得很好。非常感谢。
<ColumnDefinition Width="Auto" SharedSizeGroup="FirstColumn"/>
<ComboBox x:Name="CommandListComboBox" Grid.IsSharedSizeScope="True" ...