我想将ItemSource
a转换WPF dataGrid
为an ObservableCollection
,但是我不知道其元素的类型,所以我正在考虑使用以下代码来获取类型:
var returnedType = dg.ItemsSource.AsQueryable().ElementType;
并在此处使用returnType:
var sourceCollection = (ObservableCollection<???>)dg.ItemsSource;
更清楚地说,我想将ObservableCollection
其CollectionChanged
强制转换为因为我需要使用它。而且我在这里不能使用泛型类型,因为我想在customizad中使用它DataGrid
。
ObservableCollection<T>
实现该INotifyCollectionChanged
接口,并在此接口上定义所需的事件。因此,您可以编写如下代码:
var sourceCollection = (INotifyCollectionChanged)dg.ItemsSource;
sourceCollection.CollectionChanged += ... your delegate here ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句