简体   繁体   English

如何更改DataGrid内部的ComboBox的ItemsSource?

[英]How to change ItemsSource of ComboBox inside DataGrid?

Scenario : I have a ContentControl in my View whose Content property is bound with a DataGrid in ViewModel. 场景 :我的View中有一个ContentControl,其Content属性与ViewModel中的DataGrid绑定。 I will find out the exact number of Columns to display in DataGrid at runtime that's why I am progrmmatically creating DataGrid in ViewModel. 我将找出在运行时在DataGrid中显示的确切列数,这就是为什么我在ViewModel中以编程方式创建DataGrid的原因。 My DataGrid's ItemsSource is a DataTable. 我的DataGrid的ItemsSource是一个DataTable。 I am setting DataGrid's 'AutoGeneratingColumns' to False and I am manually creating columns. 我将DataGrid的'AutoGeneratingColumns'设置为False,并且手动创建列。 My DataGrid, along with many other columns, have two ComboBox type columns. 我的DataGrid以及许多其他列都有两个ComboBox类型的列。 I have to change ItemsSource of second ComboBox based on selected index of first ComboBox. 我必须根据第一个ComboBox的选定索引更改第二个ComboBox的ItemsSource。 So each row of my DataGrid can have different ItemsSource for second ComboBox. 因此,DataGrid的每一行可以为第二个ComboBox提供不同的ItemsSource。

My Question is how can I dynamically change ItemsSource of second ComboBox based on selected index in first ComboBox at runtime in a single row of DataGrid? 我的问题是如何在运行时在DataGrid的单行中基于第一个ComboBox中的选定索引动态更改第二个ComboBox的ItemsSource? I also would like to know if there is any better solution possible than my current approach in this scenario? 我还想知道在这种情况下是否有比我目前的方法更好的解决方案?

If you working with a viewmodel, it's not that hard. 如果您使用的是视图模型,这并不难。 Bind you comboboxes itemssources to properties on your viewmodel. 将组合框将项目源绑定到视图模型上的属性。 Bind the first combobox's selecteditem to a property on your viewmodel. 将第一个组合框的selecteditem绑定到视图模型上的属性。 In the setter of that property, change the collection of the second combobox. 在该属性的设置器中,更改第二个组合框的集合。

With viewmodels, this is the easiest method. 对于视图模型,这是最简单的方法。 Although creating datagrid columns in you view is not, you could move that to a helper class and call it from the code behind or subclass te datagrid. 尽管不是在视图中创建datagrid列,但您可以将其移至帮助器类,然后从datagrid的后面代码或子类中调用它。 With runtime columns it's hard, I know. 我知道,使用运行时列很难。

Update: What I normally do is to use normal columns when showing data, and comboboxes on the row which is selected. 更新:我通常要做的是在显示数据时使用普通列,并在选定的行上使用组合框。 That way you won't have that problem You'll have to synchronise the selecteditemproperty when the selecteditem of the datagrid changes. 这样,您就不会有问题,当数据网格的selecteditem更改时,您将必须同步selecteditemproperty。 That will also trigger the update of the itemssource of the second combobox 这也将触发第二个组合框的itemssource的更新

Another option is to make to collections of the combobox subitems in your main collection and pre-populate them. 另一种选择是在主集合中创建组合框子项的集合,并预先填充它们。 And change the collection of combobox 2 on changes of the selected item in combobox 1. But that could be potentionally be a lot work and storage. 并根据组合框1中所选项目的更改来更改组合框2的集合。但是,这很可能需要大量的工作和存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM