簡體   English   中英

WPF組合框:多列寬度

[英]WPF Combobox: Multiple Column Width

我在WPF中有一個ComboBox,其中包含list部分的多個列。 只要我明確指定列的寬度,一切都可以正常工作。 如果我為第一列的寬度指定“自動”,則網格結構將分解,並且每一行的列都不會對齊。 我想要的是第一列的寬度應盡可能固定,以便將最長行的文本固定,而另一列則可以填充其余可用空間。

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

謝謝。

為列創建一個SharedSizeGroup (替換<ColumnDefinition Width="225"/>

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

並在ComboBox上啟用SharedSizeScope

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM