访问DataGridCells之间的控制,动态级联组合框
-
13-09-2020 - |
题
我有一个datagrid,其中两个列是组合罗素(一个包含少数但不是这是问题)。
我想要,当用户更改第一个组合的值时,另一个列中的ComboBox应该绑定到其属性(此属性是集合)。说第一个ComboBox是类别,我希望当用户更改其值时,其他CB填充了(第一个组合所选类别)的值.vender。
我该怎么做,我不使用mvvm,只是简单的wpf。 我不知道什么应该是正确的实现它,我希望我开始对。
我想,如果我可以从第一个SelectionChangeHandler那里得到另一个ComboBox(它位于不同的DataGridcell),因为那么我可以在第一个选择的每个选择变化上重置它的源。 请注意,我具有到达当前(第一个)DataGridCell的能力,我只是在寻找访问正确的DataGridcell兄弟姐妹的有效方法,然后获得其子女(第二)组合。
还请注意,所选类别应因行到行而异,第二个组合框应依赖于此行的类别。
我实际上试图实现它,以便CollectionViewSource.source绑定到当前项目(即行的DataContext),但它似乎没有工作。
我更愿意通过第一个ComboBox的选择,通过动作触发器或处理程序设置第二个组合的CollectionViewSource(vendorscollection)。
在该字段中的其他组合框似乎没有问题,因为它们都彼此绑定,我可能会使用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>
. 解决方案
我刚刚遇到了你的问题。你得到了解决的问题吗?我认为你的问题类似于这个我得到了。希望解决方案也有助于您。