How to Use ComboBox Control in WPF

September 02, 2013 , , , , 0 Comments

Dropdown list provides user to select an item from the existing list of items. Combo Box is a control allowing the user to either to type a value into the control or choose from a list. It means it is a combination of textbox and a drop down list.

Combo Box can contain a collection of objects of any type, it means it is an Items Control. To add some items in combo Box through XAML:
<ComboBox Name="cmbBox">
<sys:String>First Item</sys:String>
<sys:String>Second Item</sys:String>
<sys:String>Third Item</sys:String>

The above code snippet is used to simply add string type data to combo box. By using the above code snippet a combo box with three items will be created like in following image:

ComboBox control in WPF

The same combo box can also be added dynamically using the following code snippet of c# language.
ComboBox cmbBox = new ComboBox();
cmbBox.Name = "cmbBox";
cmbBox.Items.Add("First Item");
cmbBox.Items.Add("Second Item");
cmbBox.Items.Add("Third Item");

Now add this combo box to the container to which you want. The same combo box as in above image will be generated using this code snippet. We can perform any other operation on the click event of selection changed event of combo box. Generate the selectionChanged event and write it as the following code snippet.
private void cmbBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
MessageBox.Show("ComboBox Selection Changed");

Whenever you change the selected item of this combo box, it will show a message as written in above code.

Subhash Sharma

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google