Wpf DataGridCells、动态级联组合框之间的访问控制
我有一个DataGrid,其中两列是组合框(其中一列包含很少的组合框,但这不是问题所在) 我希望,当用户更改第一个Combo的值时,另一列中的ComboBox应该绑定到它的一个属性(这个属性是一个集合)。假设第一个组合框是Category,我希望当用户更改其值时,另一个CB将填充(第一个组合的所选Category).Vendors的值 我应该怎么做,我不使用MVVM,只使用简单的WPF。 我不知道什么是正确的方法来实现它,我希望我开始的时候是正确的 我想,如果我能从第一个SelectionChangeHandler中获得另一个组合框(位于不同的DataGridCell中),那将是最好的,因为这样我就可以在第一个组合框的每次选择更改时重置其源。 请注意,我能够访问当前(第一个)DataGridCell,我只是在寻找一种有效的方法来访问正确的DataGridCell同级,然后获取其子(第二个)组合 还请注意,所选类别应因行而异,第二个组合框应取决于此行的类别。Wpf DataGridCells、动态级联组合框之间的访问控制,wpf,datagrid,combobox,cascadingdropdown,datagridcell,Wpf,Datagrid,Combobox,Cascadingdropdown,Datagridcell,我有一个DataGrid,其中两列是组合框(其中一列包含很少的组合框,但这不是问题所在) 我希望,当用户更改第一个Combo的值时,另一列中的ComboBox应该绑定到它的一个属性(这个属性是一个集合)。假设第一个组合框是Category,我希望当用户更改其值时,另一个CB将填充(第一个组合的所选Category).Vendors的值 我应该怎么做,我不使用MVVM,只使用简单的WPF。 我不知道什么是正确的方法来实现它,我希望我开始的时候是正确的 我想,如果我能从第一个SelectionCha
实际上,我尝试实现它,以便CollectionViewSource.Source绑定到当前项(即行的DataContext),但它似乎不起作用。
我更喜欢在第一个组合框的SelectionChange中通过操作触发器或处理程序设置第二个组合的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>
我刚碰到你的问题。你的问题解决了吗?我想你的问题和我得到的差不多。希望那里的解决方案也能帮助你