I have a datagrid with 2 DataGridComboxClolumn elements and want to change the ItemsSource of the second when the value of the first is changed. How can I set up such a binding?
Note: the datagrid has it’s own itemssource and the data is hierachical following:
ItemsSource of the datagrid (list op type Channel):
public class Root
{
public List<Channel> Ch { get; set; } // This is the ItemsSource of the datagrid itself
}
public class Channel
{
public Settings TheSettings { get; set; }
}
The thing I want to accomplish is that the TheSettings is set with selected value of the second ComboBox. This is easyly done by setting the ComboBox ItemsSource. Although I require that the ItemsSource of the second combox to be dynamic. E.g. it has to change to the source selected in the FIRST combobox. How can this be done?
Option 1
You can create your a DataGridTemplateColumn with two combobox. The first one can be populated with items from the main ViewModel and the second one will be bound to the items of the SelectedItem of the first.
Option 2
Create an object to wrap your Channel objects, and put in it the logic to allow one combobox to drive the items of the other:
Your Root class would then expose a collection of wrappers instead a collection of channels. Your DataGrid would have 2 ComboBoxColumns. The SelectedItem of the first would be bound to the property “ComboBox1SelectedItem” of the wrapper. The ItemsSource of the second would be bound to the property “ComboBox2ItemsSource” of the wrapper and the SelectedItem of the second column would be bound the setting of the Channel instance of the wrapper, with the path “Ch.TheSettting”.