We already saw couple of examples of combo box. Here we saw combo box inside tree view, here inside list view, here inside tab control and here inside toolbar. Now we are going to insert combo box inside the list box. By definition, it is very much it is very much similar to adding combo box inside the list view.
First we define the data structure to store the data. Here is our class to store information.
- public class States
- {
- public States()
- {
- }
- public States(String name, ObservableCollection<string> colors)
- {
- Name = name;
- Colors = colors;
- }
- public String Name
- { get; set; }
- public ObservableCollection<String> Colors
- { get; set; }
- }
Then we define the combo box inside the list box item template. We display combo box inside each list box item to display all the colors. And when user select any color from the combo box then it will change the background color of the text to the selected color. We are using Stack Panel to display the text block and the combo box inside one list box item. Here is a piece of code for this.
- <StackPanel>
- <TextBlock Margin="2" Text="{Binding Name}"
- Background="{Binding ElementName=cmbColors, Path=SelectedItem}"/>
- <ComboBox Margin="2" Name="cmbColors" ItemsSource="{Binding Colors}"
- HorizontalContentAlignment="Stretch">
- <ComboBox.ItemTemplate>
- <DataTemplate>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition Width="2*"/>
- </Grid.ColumnDefinitions>
- <Rectangle Grid.Column="0" Margin="2, 1" Fill="{Binding}"/>
- <TextBlock Grid.Column="1" Margin="2, 1" Text="{Binding}"/>
- </Grid>
- </DataTemplate>
- </ComboBox.ItemTemplate>
- </ComboBox>
- </StackPanel>
Here is complete C# code of the project.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Media.Media3D;
- using System.Collections.ObjectModel;
- using System.Drawing;
- namespace WpfCombo
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- ObservableCollection<string> colors = new ObservableCollection<string>();
- String[] allColors = Enum.GetNames(typeof(KnownColor));
- foreach (String color in allColors)
- {
- colors.Add(color);
- }
- ObservableCollection<States> states = new ObservableCollection<States>();
- states.Add(new States("California", colors));
- states.Add(new States("Maryland", colors));
- states.Add(new States("Washington", colors));
- states.Add(new States("New York", colors));
- states.Add(new States("Taxes", colors));
- list.ItemsSource = states;
- }
- }
- public class States
- {
- public States()
- {
- }
- public States(String name, ObservableCollection<string> colors)
- {
- Name = name;
- Colors = colors;
- }
- public String Name
- { get; set; }
- public ObservableCollection<String> Colors
- { get; set; }
- }
- }
Here is complete XAML code of the project.
- <Window x:Class="WpfCombo.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Combo inside Listbox" Height="400" Width="400">
- <Grid>
- <ListBox Name="list" Margin="5" HorizontalContentAlignment="Stretch">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <Border Margin="3" CornerRadius="5" BorderBrush="Brown" BorderThickness="2">
- <StackPanel>
- <TextBlock Margin="2" Text="{Binding Name}"
- Background="{Binding ElementName=cmbColors, Path=SelectedItem}"/>
- <ComboBox Margin="2" Name="cmbColors" ItemsSource="{Binding Colors}"
- HorizontalContentAlignment="Stretch">
- <ComboBox.ItemTemplate>
- <DataTemplate>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition Width="2*"/>
- </Grid.ColumnDefinitions>
- <Rectangle Grid.Column="0" Margin="2, 1" Fill="{Binding}"/>
- <TextBlock Grid.Column="1" Margin="2, 1" Text="{Binding}"/>
- </Grid>
- </DataTemplate>
- </ComboBox.ItemTemplate>
- </ComboBox>
- </StackPanel>
- </Border>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- </Grid>
- </Window>
Here is the output of the program.


[...] and create very powerful user interface with it. Here we create one combo box inside the list box here. In that example we change the background color of the text when we change the color selection from [...]
By: Traversing Visual Tree Revisited « Zeeshan Amjad's WPF Blog on January 11, 2011
at 12:02 am
Excellent! You’re posts are extremely helpful to me!
By: jerimiahbaldwin on February 9, 2011
at 12:53 pm
Thanks to like it. I am glad that these are helpful to you.
By: zamjad on February 9, 2011
at 1:30 pm
[…] the combobox. This particular task is quite simple and we already saw an example reverse of it here where we display combobox inside the listbox. The basic idea is same that is created an […]
By: ListBox in ComboBox | Zeeshan Amjad's WinRT, and WPF Blog on May 15, 2013
at 8:14 pm