WPF ComboBox 多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23084576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:04:58  来源:igfitidea点击:

WPF ComboBox Multiple Columns

wpfxamlcombobox

提问by user3428422

I am just wondering if there is a wpf combobox control that can contain multiple columns?

我只是想知道是否有可以包含多列的 wpf 组合框控件?

And if not, what XAML I need to use to achieve this?

如果没有,我需要使用什么 XAML 来实现这一目标?

I am just looking for a basic two column combobox if is possible,

如果可能的话,我只是在寻找一个基本的两列组合框,

Thanks

谢谢

回答by Heena Patil

Please Refer these links for Multiple Column Comboboxwhich is implemented by editing combox and comboboxitem Default template/style.

请参阅通过编辑组合框和组合框项目默认模板/样式实现的多列组合框的这些链接。

1)Link1

2)Link2

1)链接1

2)链路2

Xaml code: Please take a look at commented Trigger IsHighlightedin ComboboxItem style

Xaml 代码:请查看ComboboxItem 样式中注释的Trigger IsHighlighted

 <Grid>
    <ComboBox Height="30" Margin="5" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Margin="2" Text="{Binding Name}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">                
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid x:Name="gd" TextElement.Foreground="Black">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Margin="5" Grid.Column="0" Text="{Binding Name}"/>
                                <TextBlock Margin="5" Grid.Column="1" Text="{Binding State}"/>
                                <TextBlock Margin="5" Grid.Column="2" Text="{Binding Population}"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="ComboBoxItem.IsSelected" Value="True">
                                    <Setter TargetName="gd"  Property="Background" Value="Gray"></Setter>
                                    <Setter TargetName="gd"  Property="TextElement.Foreground" Value="White"></Setter>
                                </Trigger>
                                <Trigger Property="ComboBoxItem.IsMouseOver" Value="True">
                                    <Setter TargetName="gd"  Property="Background" Value="Blue"></Setter>
                                    <Setter TargetName="gd"  Property="TextElement.Foreground" Value="White"></Setter>
                                </Trigger>

                                <!--IsHighlighted and IsMouseOver is showing same effect but IsHighlighted is used for showing logical focus( for understanding check using tab key)-->

                                <!--<Trigger Property="ComboBoxItem.IsHighlighted" Value="True">
                                    <Setter TargetName="gd"  Property="Background" Value="Yellow"></Setter>
                                    <Setter TargetName="gd"  Property="TextElement.Foreground" Value="Black"></Setter>
                                </Trigger>-->
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>
</Grid>

c# code

代码

public partial class MainWindow : Window

{

    private ObservableCollection<City> cities = new ObservableCollection<City>();

    public MainWindow()
    {
        InitializeComponent();
        cities.Add(new City() { Name = "Mumbai", State = "Maharashtra", Population = 3000000 });
        cities.Add(new City() { Name = "Pune", State = "Maharashtra", Population = 7000000 });
        cities.Add(new City() { Name = "Nashik", State = "Maharashtra", Population = 65000 });
        cities.Add(new City() { Name = "Aurangabad", State = "Maharashtra", Population = 5000000 });
        DataContext = cities;
    }
}

class City
{
    public string State { get; set; }
    public string Name { get; set; }
    public int Population { get; set; }
}

Outputenter image description here

输出在此处输入图片说明

回答by Pollitzer

Because I found, Heena, that your Xaml does not provide selected dropped down items to be highlighted I modified your code as follows:

因为我发现,Heena,您的 Xaml 没有提供要突出显示的选定下拉项,所以我修改了您的代码,如下所示:

Xaml

xml

<ComboBox Name="cbCities" Height="30" Margin="5" HorizontalContentAlignment="Left" HorizontalAlignment="Stretch" ItemsSource="{Binding}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Margin="2" Text="{Binding Name}"/>
                <TextBlock Margin="2" Text="{Binding State}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Border Name="templateBorder" Padding="2" SnapsToDevicePixels="true">
                            <ContentPresenter>
                                <ContentPresenter.Content>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition/>
                                            <ColumnDefinition/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Margin="5" Grid.Column="0" Text="{Binding Name}"/>
                                        <TextBlock Margin="5" Grid.Column="1" Text="{Binding State}"/>
                                        <TextBlock Margin="5" Grid.Column="2" Text="{Binding Population}"/>
                                    </Grid>
                                </ContentPresenter.Content>
                            </ContentPresenter>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsHighlighted" Value="True">
                                <Setter Property="Foreground" Value="{x:Static SystemColors.HighlightTextBrush}"/>
                                <Setter TargetName="templateBorder" Property="Background" Value="{x:Static SystemColors.HighlightBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>  

C#

C#

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    cities.Add(new City() { Name = "Boston", State = "MA", Population = 3000000 });
    cities.Add(new City() { Name = "Los Angeles", State = "CA", Population = 7000000 });
    cities.Add(new City() { Name = "Frederick", State = "MD", Population = 65000 });
    cities.Add(new City() { Name = "Houston", State = "TX", Population = 5000000 });
    cbCities.DataContext = cities;
}

class City
{
    public string State { get; set; }
    public string Name { get; set; }
    public int Population { get; set; }
}  

Output

输出

enter image description here

在此处输入图片说明