【发布时间】:2016-04-05 13:31:58
【问题描述】:
我有一个带有 class1 类型对象的 SelectedObject 的 PropertyGrid。
我正在为class1 对象实现ICustomTypeDescriptor 接口,并且我从另一个对象class2 获得PropertyDescriptorCollection,并且需要在PropertyGrid 中显示class2 PropertyDescriptors 以及class1PropertyDescriptors。
我在 class2 PropertyDescriptors 的 PropertyGrid 中显示以下错误:
对象与目标类型不匹配。
这是我在没有class2 PropertyDescriptors 的情况下工作的代码:
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(this);
var propertyDescriptors = new List<PropertyDescriptor>();
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
{
propertyDescriptors.Add(propertyDescriptorCollection[i]);
}
return new PropertyDescriptorCollection(propertyDescriptors.ToArray());
}
这是我正在处理的代码:
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(this);
var propertyDescriptors = new List<PropertyDescriptor>();
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
{
propertyDescriptors.Add(propertyDescriptorCollection[i]);
}
var class2PropertyDescriptorCollection = TypeDescriptor.GetProperties(class2);
for (int i = 0; i < class2PropertyDescriptorCollection.Count; i++)
{
propertyDescriptors.Add(class2PropertyDescriptorCollection[i]);
}
return new PropertyDescriptorCollection(propertyDescriptors.ToArray());
}
当来自class1 的PropertyDescriptors 显示在PropertyGrid 中时,如何显示来自class2 的PropertyDescriptors?
【问题讨论】:
标签: c# propertydescriptor icustomtypedescriptor