Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I have two combo box, and need to set the values of latter combobox relative to the former combo box.
for eg.,
Combobox(I) - values A, B & C
ComboBox(II) - values 0, 1, 2 ,3,4,5,6,7,8,9

On user Selection of (I) and Type A- (II) - values 0,1,2
On user Selection of (I) and Type B- (II) - values 1...9
On User selection of (I) and Type C- (III) - values 9 Only, needs selection.

How to effect the said changes. Any help is appreciable. Thanks.,



With Regards,
Samanth_90
Posted

1 solution

You need to have a SelectionChanged event handler :
XAML code :
HTML
<combobox height="23" horizontalalignment="Left" margin="12,41,0,0" name="comboBox1" verticalalignment="Top" width="120" selectionchanged="comboBox1_SelectionChanged">
            <comboboxitem content="A" />
            <comboboxitem content="B" />
            <comboboxitem content="C" />
        </combobox>
        <combobox height="23" horizontalalignment="Left" margin="12,84,0,0" name="comboBox2" verticalalignment="Top" width="120" />


Code behind :

in Loaded event handler (of your Window or Page or User Control) :
C#
comboBox1.SelectedIndex = 0;



and the SelectionChanged event handler :
C#
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (comboBox2 == null)
        return;

    switch (comboBox1.SelectedIndex)
    {
        case 0:
            comboBox2.ItemsSource = new List<int> { 0, 1, 2 };
            break;
        case 1:
            comboBox2.ItemsSource = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            break;
        case 2:
            comboBox2.ItemsSource = new List<int> { 9 };
            break;
    }

    comboBox2.SelectedIndex = 0;
}



Hope it helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900