WPF Combobox worked as a dropdown same as the HTML one. It’s a kind of selection UI component, the user can select single or multiple values from the Combobox. Basically, it is a list containing information to get input from the user in form of selection.
WPF Combobox by default display nothing if already selected, else it will show the selected item of the list. There are two ways to add Combobox to your WPF application, either you drag and drop components from the toolbox or write the xaml code for it.
<ComboBox x:Name="comboBox"/>
The above code will generate the WPF ComboBox in your application. Right now it’s just empty, so we need to fill some data inside it, which you can do either in xaml or in C#.
Add list items in WPF Combobox by writing xaml code.
<ComboBox x:Name = "comboBox"> <ComboBoxItem Content = "Apple" /> <ComboBoxItem Content = "Banana" /> <ComboBoxItem Content = "Cake" /> </ComboBox>
Add list items in WPF Combobox by writing C# code.
public MainWindow() { InitializeComponent(); comboBox.Items.Add("Apple"); comboBox.Items.Add("Banana"); comboBox.Items.Add("Cake"); }
Output:
If you want to select any specific list item in xaml you can use the “Selector.IsSelected” property marked as true, which you can observe as follows.
<ComboBox x:Name = "comboBox"> <ComboBoxItem Content = "XAML Added - Apple" Selector.IsSelected="True"/> <ComboBoxItem Content = "XAML Added - Banana" /> <ComboBoxItem Content = "XAML Added - Cake" /> </ComboBox>
You can do the same thing in C# also, meaning select the WPF Combobox item in C#, which you can observe in the following code.
Remember, in C# whenever you want to manipulate any UI component, you need to call that by the name which you declare in XAML, in our case it’s “comboBox”. Names of WPF UI components are case-sensitive.
comboBox.SelectedIndex = 1;
If you want to make your Combobox list item editable by the user you can do that by using “IsEditable” property.
<ComboBox x:Name = "comboBox" IsEditable = "True"> <ComboBoxItem Content = "XAML Added - Apple" Selector.IsSelected="True"/> <ComboBoxItem Content = "XAML Added - Banana" /> <ComboBoxItem Content = "XAML Added - Cake" /> </ComboBox>
Now all the items in the lists are editable by the user, and the user can modify the values during selection. You can do the same thing in C# as follows.
comboBox.IsEditable = true;
ComboBox gives you three basic events on which you can manipulate its value.
- Selected – It will trigger whenever the user will select any list item as input.
- Unselected – It will trigger whenever the user will deselect any list item as input.
- SelectionChanged – It will trigger whenever the user will select or deselect any list item.
private void comboBox_Selected(object sender, RoutedEventArgs e) { //write your business logic here, perform action when combobox do selection } private void comboBox_Unselected(object sender, RoutedEventArgs e) { //write your business logic here, perform action when combobox do unselection } private void comboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { //write your business logic here, perform action when combobox do selection/unselection }
Note: You can apply all the Mouse Events & Keyboard Events on this component.