如何获取绑定到DataGrid的对象类型 [英] How to get Object type bound to DataGrid
问题描述
我有一个绑定到ObservableCollection的DataGrid.
I have a DataGrid that is bound to ObservableCollection.
我想知道的是:在不求助于项目和检索对象类型的情况下,我能以某种方式使用实际的DataGrid对象和ItemSource来找到对象的类型吗?
What I am wondering is: without resorting to looking at an item and retrieving the object type, can I somehow use the actual DataGrid object and the ItemSource to find the type of objects?
所以,如果我有以下内容:
So if I have the following:
DataGrid dg = DataGridObject as DataGrid;
Console.WriteLine("binding5=" + dg.ItemsSource.GetType());
output = System.Collections.ObjectModel.ObservableCollection`1[UserManagement.UserViewModel]
我可以将 UserManagement.UserViewModel
提取到对象变量中
Can I extract UserManagement.UserViewModel
into an object variable somehow
推荐答案
如果我对您的理解正确,那么您想找出对象内部 的类型,该对象被设置为DataGrid.ItemsSource
属性.为此,您可以使用一些基本的反射.试试这个:
If I understand you correctly, you want to find out the type of object inside the collection that is set as the DataGrid.ItemsSource
property. To do this, you can use some basic reflection. Try this:
var collection = ListBox.ItemsSource;
Type collectionType = collection.GetType();
Type itemType = collectionType.GetGenericArguments().Single();
这篇关于如何获取绑定到DataGrid的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!