2
votes

I have multiple combo boxes (to be specific: 3) and every combo box value depends on the selected value of previous combobox. Any suggestions on how to implement this in WPF?

Every Combo box has list<> as its item source.

3

3 Answers

2
votes

Let us assume that the names of your comboboxes are comboBox1,comboBox2 and comboBox3 Below I am giving example of only one event. On basis of this you can fire events for all your comboboxes.

comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);

This is the event call

void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    comboBox2.SelectedIndex = comboBox1.SelectedIndex;
//do other work here
}
1
votes

I do have something similar in my application. You have to use ObservableCollection<> instead of List<>.

I have 2 ComboBoxes where you can choose to "group" a result in the first ComboBox and in the second one you can go into more detail if you want to.

First one has "Security, Country, Sector" and second one can have a list of securities, countries or sectors...depending on the first selection.

To do this I have bound the comboboxes to ObservableCollection. When the user selects something in ComboBox1 i will know it from the SelectionChanged Event and will fill the ObservableCollection for ComboBox2 with Countries, Sectors or Securities.

The ObservableCollection<> will then "automatically" inform the comboBox2 that there are new items and it will refresh.

Oh, for this to work you have to do it with the MVVM pattern...but I assume you are allready using it.

0
votes

It should be something like:

<ComboBox x:Name="cbModels"
 ItemsSource="{Binding ElementName=comboBox1,Path=SelectedItem.Orders}"
 DisplayMemberPath="OrderId"/>

the SelectedItem.Orders displays the orders of the customer selected in comboBox1.