In this topic, you will populate a combo box's drop-down list with a collection.
Complete the following steps:
1. Open the MainPage.xaml.cs page.
2. Import the following namespace into the project:
Imports System.Collections.Generic
•C#
using System.Collections.Generic;
3. Create your list by adding the following code beneath the InitializeComponent() method:
Dim dropDownList As New List(Of String)()
dropDownList.Add("C1ComboBoxItem1")
dropDownList.Add("C1ComboBoxItem2")
dropDownList.Add("C1ComboBoxItem3")
dropDownList.Add("C1ComboBoxItem4")
•C#
List<string> dropDownList = new List<string>();
dropDownList.Add("C1ComboBoxItem1");
dropDownList.Add("C1ComboBoxItem2");
dropDownList.Add("C1ComboBoxItem3");
dropDownList.Add("C1ComboBoxItem4");
4. Add the list to the combo box by setting the ItemsSource property:
C1ComboBox1.ItemsSource = dropDownList
•C#
c1ComboBox1.ItemsSource = dropDownList;
5. Run the program.
This Topic Illustrates the Following:
With the project running, click the drop-down arrow and observe that four items appear in the drop-down list. The result resembles the following image: