我定制的对象如何类型显示,在一个 PropertyGrid 通过实施 ICustomTypeDescriptor.我允许用户创建自己的定义属性,都存储在一个单一的字典键的和价值观。我能够创造一切 PropertyDescriptors 对于这些价值观,并查看他们在酒店的网格。然而,我还想显示所有对默认的性质,否则将显示,如果 PropertyGrid 人口通过的反映而不是我的复盖 ICustomTypeDescriptor.GetProperties 法。

现在我知道如何获得的类型的对象,然后 GetProperties(), 但这返回一组 PropertyInfoProperyDescriptor.所以我怎么可以转换的 PropertyInfo 目的类型 PropertyDescriptor 对象包括进入我的收集定义 PropertyDescriptors?

//gets the local intrinsic properties of the object
Type thisType = this.GetType();
PropertyInfo[] thisProps = thisType.GetProperties();

//this line obviously doesn't work because the propertydescriptor 
//collection needs an array of PropertyDescriptors not PropertyInfo
PropertyDescriptorCollection propCOl = 
    new PropertyDescriptorCollection(thisProps);
有帮助吗?

解决方案

PropertyDescriptorCollection props = TypeDescriptor.GetProperties(thisType);

作为一个旁白:这不会包括你 ICustomTypeDescriptor 定制的,但是它 包括任何定制通过 TypeDescriptionProvider.

(编辑) 作为第一边-你还可以调整 PropertyGrid 通过提供一个 TypeConverter -简单得多比 ICustomTypeDescriptorTypeDescriptionProvider -例如:

[TypeConverter(typeof(FooConverter))]
class Foo { }

class FooConverter : ExpandableObjectConverter
{
    public override PropertyDescriptorCollection GetProperties(
       ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        // your code here, perhaps using base.GetPoperties(
        //    context, value, attributes);
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top