I came across one simple application written in MS Access. In that application there is one combo box that display multiple columns when open. I tried to do the same thing in WPF and guess that it would be an easy task just define the data template for ItemTemplate. Here is my first attempt of this.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Multi Column ComboBox" Height="300" Width="400">
<Grid>
<ComboBox Height="30" Margin="5" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Margin="2" Grid.Column="0" Text="{Binding Name}"/>
<TextBlock Margin="2" Grid.Column="1" Text="{Binding State}"/>
<TextBlock Margin="2" Grid.Column="2" Text="{Binding Population}"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
This code in fact display multiple columns, but this is not exactly what I want. This code also display multiple column for selected item.Take a look at the screen shot of this.
I want to display only one value as a selected value, but multiple columns when open the combo box. There are multiple ways to do this. The first solution came to my mind is doing condition template selection which we already did several time. But for that we have to create a new class. I was looking for a solution where I don’t have to create a new class, in other words if I can do this with XAML only.
I was thinking may be I can use both data template and control template together, one for the selected item and other for the items when we drop downed the combo box. And the closest I can get is ItemContainerStyle property. I define the control template of container, it means I define the control template when we open the combo box and define data template when we don’t open the combo box.
Here is my code to do this.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Multi Column ComboBox" Height="300" Width="400">
<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>
<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>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</Grid>
</Window>
Here is complete C# code of the program.
using System.Collections.ObjectModel;
namespace WpfCombo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<City> cities = new ObservableCollection<City>();
public MainWindow()
{
InitializeComponent();
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 });
DataContext = cities;
}
}
class City
{
public string State
{ get; set; }
public string Name
{ get; set; }
public int Population
{ get; set; }
}
}
Here is the output of the program.
Thanks for the very nice post.
but I have a question.
How about the string being returned by the Combobox.Seleteditem? It’s just returning the Name of Class city, not really the selected text. Would it be posible to get all the strings shown on the dropdown “selected “item?
E.X. If I Select Boston, I must also be able to get the “MA” and “300000”
Thank you
By: rick on November 17, 2012
at 5:43 am
Hi rick
Thanks to like the post. Please correct me if i misunderstood your question. If we get the SelectedItem then we will get the class of State and from where I can get rest of the information.
Regards
Zeeshan Amjad
By: Zeeshan Amjad on November 28, 2012
at 10:11 am
Hello Zeeshan,
Nice article! I need to do the same but with datagrid combobox. I need to show a combo box in the datagrid with two or three values. Do you have any code that can help me out? How different would this be from the code you are showing here?
Thanks so much, really appreciate your help!!
By: Guisselle on May 3, 2013
at 8:04 am
Hi.
Thanks to like my article. I actually wrote an article to discuss how insert combo box inside the grid. You can take a look at this here
You can use the same technique discuss here https://zamjad.wordpress.com/2012/08/15/multi-columns-combo-box/ to define multiple column in combo box.
Thanks and regards
Zeeshan Amjad
By: Zeeshan Amjad on May 15, 2013
at 8:20 pm
[…] we have different presentation in combo box and selected item by using ItemContinerStyle property here. In that example we display multiple column in combo box but display only one data when we select […]
By: ListBox in ComboBox | Zeeshan Amjad's WinRT, and WPF Blog on May 15, 2013
at 8:14 pm
Hi zeeshan,
Is it possible to show the column header in this.
I want to display header for each column present in combobox.
can u please help me.
regards
azar
By: azar on December 2, 2013
at 4:30 am
Excellent
Thanking you Very Much
By: TSN Prasad on January 1, 2015
at 4:41 am
Thanks to like it.
By: Zeeshan Amjad on April 1, 2015
at 9:14 pm
Hello Zeesha this helped me a lot!
I have one question left – why the name-Property is not shown in the ComboBox when a Item was selected with IsEditable=true? If IsEditable=false everything works perfect.
Thanks and Greetings
hanz
By: hanz on July 28, 2015
at 9:25 am
I just found the answer:
http://www.shujaat.net/2010/08/wpf-editable-combobox-with-datatemplate.html
Greetings
hanz
By: hanz on July 28, 2015
at 9:41 am