SelectedObject = new DictionaryPropertyGridAdapter(dicTest); } class DictionaryPropertyGridAdapter : ICustomTypeDescriptor DictionaryPropertyGridAdapter(IDictionary d) { _dictionary = d; } //Three of the ICustomTypeDescriptor TypeDescriptor.GetEvents(this, attributes, true); } EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents GetDefaultProperty() { return null; } PropertyDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetProperties () { return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]); } //Then
解决方法 在.Net中提供了一个自定义类型说明的接口(System.ComponentModel.ICustomTypeDescriptor),PropertyGrid可以直接自动处理用此接口生成的对象 在写ICustomTypeDescriptor接口时,其他的一些方法可以用TypeDescriptor直接返回相关方法调用,并在GetPropertyOwner方法中应返回当前选择对象否则将不会对修改值起任何作用
以前做Delegate实现的时候只接触过成员函数指针, 没想到还有个成员变量指针, C++真是博大精深 Enum和Container需要特殊对待 如果要反射到.net的PropertyGrid, 可以使用ICustomTypeDescriptor
如下面的代码片断所示,AssociatedValidatorProvider定义一个受保护的虚方法GetTypeDescriptor用于获取指定类型的描述对象(其类型实现了接口ICustomTypeDescriptor AssociatedValidatorProvider : ModelValidatorProvider 2: { 3: protected virtual ICustomTypeDescriptor
ctor() void ControlDesigner..ctor() Class Netron.GraphLib.PropertyBag: Object System.ComponentModel.ICustomTypeDescriptor.GetEditor ControlDesigner..ctor() Not Specified Class Netron.GraphLib.PropertyBag: Object System.ComponentModel.ICustomTypeDescriptor.GetEditor
CustomTypeDescriptor 提供 ICustomTypeDescriptor 接口的简单默认实现。 ICustomTypeDescriptor 提供为对象提供动态自定义类型信息的接口。 IDataErrorInfo 提供功能,该功能提供用户界面可以绑定的自定义错误信息。
WPF中为DataTable生成视图模型的流程图 重点在于DataTable 实现 IListSource接口,并构造动态的视图动态类型 DataRowView并使其实现ICustomTypeDescriptor 主要也是让 EntityList 实现 IListSource接口,并添加 EntityView 类实现 ICustomTypeDescriptor 接口,这样,就可以实现动态属性的WPF绑定了。