Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

xaml - WPF ComboBox Multiple Columns

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

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

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

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

1)Link1

2)Link2

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

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

Output enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...