I have a DataGrid that two of its columns are ComboBoxes (one contains several, but this is not a problem).
I want that when the user changes the first value of Combo, the ComboBox in another column should bind to its property (this property is a collection). Let's say First ComboBox is a category, I want that when a user changes his value, another CB is filled with values (the first combo is the selected category) .Vendors.
How do I do this, I am not using MVVM, just plain WPF. I do not know what the correct way to implement it should be. Hope I started correctly.
I think if I could get another ComboBox (which is in another DataGridCell) from the first SelectionChangeHandler, which would be better, because then I can reset its source every time I change the selection of the first. Please note that I have the opportunity to reach the current (first) DataGridCell, I'm just looking for an effective way to access the correct DataGridCell sibling, and then get its child (second) combo.
Also note that the selected category should differ from row to row, and the second ComboBox should depend on this row category.
I actually tried to implement it so that CollectionViewSource.Source is bound to the current element (i.e. the DataContext String), but it does not seem to work.
I prefer to set the second combo CollectionViewSource (VendorsCollection) through an action trigger or handler in the first SelectionChange for the ComboBox.
The other ComboBoxes in this field do not seem to pose a problem, since they are all related to each other, I can use CollectionViewSource.Filter, anyway, this is not a problem to access them, because they are simple siblings, and not like the first , which is a distant cousin located deep in another DataGridCell.
:
<DataGrid>
<DataGrid.Resources>
<CollectionViewSource x:Key="CategoriesCollection" Source="{Binding Context.CategoriesList, Source={x:Static Application.Current}, IsAsync=True}" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Category">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding Category}" Text="{Binding Title}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
IsSynchronizedWithCurrentItem="False"
ItemsSource="{Binding Source={StaticResource CategoriesCollection}}"
DisplayMemberPath="Title"
SelectionChanged="cbCategories_SelectionChanged"
SelectedItem="{Binding Category}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Style">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock DataContext="{Binding Finish.Style.Vendor}" Text="{Binding Contact.Title}"/>
<TextBlock DataContext="{Binding Finish.Style}" Text="{Binding Title}"/>
<TextBlock Text="{Binding Finish.Title}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<StackPanel.Resources>
<CollectionViewSource x:Key="VendorsCollection" Source="{Binding Vendors, Source={StaticResource CategoriesCollection}}" />
<CollectionViewSource x:Key="StylesCollection" Source="{Binding Styles, Source={StaticResource VendorsCollection}}" />
<CollectionViewSource x:Key="FinishesCollection" Source="{Binding Finishes, Source={StaticResource StylesCollection}}" />
</StackPanel.Resources>
<ComboBox
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource VendorsCollection}}"
SelectedItem="{Binding Finish.Style.Vendor}"
DisplayMemberPath="Contact.Title"/>
<ComboBox
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource StylesCollection}}"
SelectedItem="{Binding Finish.Style}"
DisplayMemberPath="Title"/>
<ComboBox
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource FinishesCollection}}"
SelectedItem="{Binding Finish}"
DisplayMemberPath="Title"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>