访问DataGridCells之间的控件,动态级联ComboBoxes [英] Accessing control between DataGridCells, dynamic cascading ComboBoxes
问题描述
我有一个DataGrid的两个列是ComboBoxes(一个包含少数但不是这是问题)。
I have a DataGrid that two of its columns are ComboBoxes (one contains few but not this is the problem).
我想,当用户更改首先Combo的值,ComboBox在其他列应该绑定到其属性(这个属性是一个集合)。说的第一个ComboBox是类别,我想当用户更改其值时,另一个CB填充(第一个组合的所选类别).Vendors的值。
I want, that when the user changes the first Combo's value, the ComboBox in the other column should bind to a property of its (this property is a collection). Say the First ComboBox is Category, I want that when the user changes its value, the other CB is populated with the values of (first combo's selected category).Vendors.
我应该怎么做,我不使用MVVM,只是简单的WPF。
我不知道应该是什么正确的方式来实现它,我希望我开始正确。
How should I do it, I don't use MVVM, just simple WPF. I don't know what should be the right way to implement it, I hope I started it right.
我想,如果我能得到另一个ComboBox(它位于不同的DataGridCell)从第一个SelectionChangeHandler将是最好的,因为然后我可以重置其源第一个选择更改的源。
注意,我有能力到达当前(第一个)DataGridCell,我只是寻找一个有效的方式访问正确的DataGridCell兄弟,然后得到它的子(第二)组合。
I think, if I could get the other ComboBox (which is located in a different DataGridCell) from the first's SelectionChangeHandler that would be the best, because then I can reset its source on each selection change of the first one. Note that I have the capability of reaching the current (the first's) DataGridCell, I am just looking for an efficient way to access the right DataGridCell sibling and then get its child (second) combo.
另外请注意,所选类别应该在行与行之间变化,第二个ComboBox应该依赖于该行的类别。
我实际上试图实现它,使CollectionViewSource .Source绑定到当前项目(即行的DataContext),但它似乎不工作。
我更喜欢通过一个Action触发器或处理程序设置第二个组合的CollectionViewSource(VendorsCollection)在第1 ComboBox的SelectionChange。
Also note that the selected category should vary from row to row, and the second ComboBox should depend on this row's category.
I actually tried to implement it so that the CollectionViewSource.Source is bound to the current item (i.e. the row's DataContext) but it doesn't seem to work.
I prefer to set the second combo's CollectionViewSource (VendorsCollection) thru an Action trigger or handler at the 1st ComboBox's SelectionChange.
该字段中的其他ComboBoxs似乎没有问题,因为他们都是绑定在一起的,我可能使用CollectionViewSource.Filter,反正它不是一个问题,访问他们,因为他们是简单的兄弟姐妹,而不是像第一个是一个遥远的表弟位于深处另一个DataGridCell。
The other ComboBoxes in that field don't seem to make a problem as they're all bound to each other, I might use CollectionViewSource.Filter, anyway it's not a problem to access them as they are simple siblings, not like the first one which is a distant cousin located deep in another DataGridCell.
这是什么是什么尝试到目前为止:
Here is what is what I tried so far:
<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>
推荐答案
你的问题解决了吗?我认为您的问题类似于这一个我得到。希望这里的解决方案也能帮助你。
I just came across your questions. Did you get your problem resolved? I think your question is similar to this one I got. Hope the solution there helps you too.
这篇关于访问DataGridCells之间的控件,动态级联ComboBoxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!