2 つの列が ComboBoxes である DataGrid があります (1 つにはほとんど含まれていませんが、これは問題ではありません)。
ユーザーが最初の Combo の値を変更すると、他の列の ComboBox がそのプロパティにバインドされる必要があります (このプロパティはコレクションです)。最初の ComboBox がカテゴリであるとします。ユーザーがその値を変更すると、他の CB に (最初のコンボの選択されたカテゴリ) ベンダーの値が取り込まれます。
どうすればいいですか、MVVMは使用せず、単純なWPFのみです。それを実装する正しい方法が何であるかわかりません。正しく開始したことを願っています。
最初の SelectionChangeHandler から他の ComboBox (別の DataGridCell にある) を取得できれば、最初の選択が変更されるたびにそのソースをリセットできるので、それが最善だと思います。現在の (最初の) DataGridCell に到達する機能があることに注意してください。適切な DataGridCell 兄弟にアクセスして、その子 (2 番目) のコンボを取得する効率的な方法を探しているだけです。
また、選択されたカテゴリは行ごとに異なる必要があり、2 番目の ComboBox はこの行のカテゴリに依存する必要があることに注意してください。
CollectionViewSource.Source が現在のアイテム (つまり、行の DataContext) にバインドされるように実際に実装しようとしましたが、うまくいかないようです。
私は、2 番目のコンボの CollectionViewSource (VendorsCollection) を、1 番目の ComboBox の SelectionChange でアクション トリガーまたはハンドラーを介して設定することを好みます。
そのフィールドの他の ComboBoxes はすべて互いにバインドされているため、問題はないようです。CollectionViewSource.Filter を使用することもできますが、最初のものとは異なり、単純な兄弟であるため、それらにアクセスすることは問題ではありません。これは、別の 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>
<!--This is the first ComboBox-->
<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>
<!--I want, that when the user selects a value in the first ComboBox,
the VendorsCollection below should be populated with the selected Category.Vendors,
or alternatively current row's data item.Category.Vendors,
I just donno how to access current row from these 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>