简体   繁体   English

WPF组合框:多列宽度

[英]WPF Combobox: Multiple Column Width

I have a ComboBox in WPF that contains multiple columns for the list section. 我在WPF中有一个ComboBox,其中包含list部分的多个列。 Everything works fine as long as I explicitly specify the width of the columns. 只要我明确指定列的宽度,一切都可以正常工作。 If I specify "auto" for the width of the first column the grid structure breaks down and columns of each row are not aligned with each other. 如果我为第一列的宽度指定“自动”,则网格结构将分解,并且每一行的列都不会对齐。 What I would like is the ability for the first column to be as wide as it needs to be in order to fix the text for the longest row, and the other column to fill the rest of the available space. 我想要的是第一列的宽度应尽可能固定,以便将最长行的文本固定,而另一列则可以填充其余可用空间。

My XAML is below: 我的XAML如下:

<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>

Thanks. 谢谢。

create a SharedSizeGroup for columns (replacement for <ColumnDefinition Width="225"/> ) 为列创建一个SharedSizeGroup (替换<ColumnDefinition Width="225"/>

<ColumnDefinition Width="Auto" SharedSizeGroup="FirstColumn"/>

and enable SharedSizeScope on ComboBox: 并在ComboBox上启用SharedSizeScope

<ComboBox x:Name="CommandListComboBox" Grid.IsSharedSizeScope="True" ...

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

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