-->

Friday, September 6, 2013

How to Bind ComboBox with List: WPF

How to Bind ComboBox with List: WPF

The same procedure will be follow up as in listbox binding. In the XAML define a combobox with some common properties you want as height, width and etc. Don’t forget the name property, because it will be used to assign itemsSource for this control.

<ComboBox Name="comboBox" Width="200"/>

Create a list of type string and add some items with Add() method of list. I have added the same items as in grid resources.
List<string> strList = new List<string>();
strList.Add("London");
strList.Add("Italy");
strList.Add("California");
strList.Add("France");
comboBox.ItemsSource = strList;

Just run the code and the same window will be shown with a combobox containing above four items.

Combobox binding with List control WPF c#

Combobox also have a property selectedIndex which is used to get or set the selected index of the control. To select an item by default we can use this property as:

comboBox.SelectedIndex = 2;

Write the above line of code just below the c# code, and California will be selected.

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved