Step 3 of 4: Adding Code to the Control
In This Topic
In the last step, you added items to the first combo box. In this step, you will add code to the project that will populate the second combo box according to the option the user selects in the first combo box.
- Select the first C1ComboBox control (“Category”).
- In the Properties window, click the Events button.
- Double-click the inside the SelectedIndexChanged text box to add the C1ComboBox1_SelectedIndexChanged event handler. The MainPage.xaml.cs page opens.
- Import the following namespace into your project:
Visual Basic |
Copy Code
|
Imports System.Collections.Generic
|
C# |
Copy Code
|
using System.Collections.Generic;
|
- Add the following code to the C1ComboBox1_SelectedIndexChanged event handler:
Visual Basic |
Copy Code
|
'Create List for Comedy selection
Dim dropDownList_Comedy As New List(Of String)()
dropDownList_Comedy.Add("Absolutely Fabulous")
dropDownList_Comedy.Add("The Colbert Report")
dropDownList_Comedy.Add("The Daily Show")
dropDownList_Comedy.Add("The Office")
'Create List for Drama selection
Dim dropDownList_Drama As New List(Of String)()
dropDownList_Drama.Add("Breaking Bad")
dropDownList_Drama.Add("Desperate Housewives")
dropDownList_Drama.Add("Mad Men")
dropDownList_Drama.Add("The Sopranos")
'Create List for Science Fiction selection
Dim dropDownList_SciFi As New List(Of String)()
dropDownList_SciFi.Add("Battlestar Galactica")
dropDownList_SciFi.Add("Caprica")
dropDownList_SciFi.Add("Stargate")
dropDownList_SciFi.Add("Star Trek")
'Check for SelectedIndex value and assign appropriate list to 2nd combo box
If Category.SelectedIndex = 0 Then
Shows.ItemsSource = dropDownList_Comedy
ElseIf Category.SelectedIndex = 1 Then
Shows.ItemsSource = dropDownList_Drama
ElseIf Category.SelectedIndex = 2 Then
Shows.ItemsSource = dropDownList_SciFi
End If
|
C# |
Copy Code
|
//Create List for Comedy selection
List<string> dropDownList_Comedy = new List<string>();
dropDownList_Comedy.Add("Absolutely Fabulous");
dropDownList_Comedy.Add("The Colbert Report");
dropDownList_Comedy.Add("The Daily Show");
dropDownList_Comedy.Add("The Office");
//Create List for Drama selection
List<string> dropDownList_Drama = new List<string>();
dropDownList_Drama.Add("Breaking Bad");
dropDownList_Drama.Add("Desperate Housewives");
dropDownList_Drama.Add("Mad Men");
dropDownList_Drama.Add("The Sopranos");
//Create List for Science Fiction selection
List<string> dropDownList_SciFi = new List<string>();
dropDownList_SciFi.Add("Battlestar Galactica");
dropDownList_SciFi.Add("Caprica");
dropDownList_SciFi.Add("Stargate");
dropDownList_SciFi.Add("Star Trek");
//Check for SelectedIndex value and assign appropriate list to 2nd combo box
if (Category.SelectedIndex == 0)
{
Shows.ItemsSource = dropDownList_Comedy;
}
else if (Category.SelectedIndex == 1)
{
Shows.ItemsSource = dropDownList_Drama;
}
else if (Category.SelectedIndex ==2)
{
Shows.ItemsSource = dropDownList_SciFi;
}
|
In the next step, you will run the project and observe the results of this quick start.