首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向PropertyDescriptor添加类别属性

向PropertyDescriptor添加类别属性
EN

Stack Overflow用户
提问于 2009-05-05 19:04:51
回答 1查看 6.9K关注 0票数 1

我有一组自定义的PropertyDescriptor,我还想添加类别,以便它们在PropertyGrid中以更有组织的方式显示。我希望每种类型的PropertyDescriptor都归入一个特定的类别。

我尝试使用TypeDescriptor.AddAttributes()向现有类别添加属性,但没有添加PropertyDescriptor属性。

代码语言:javascript
复制
CategoryAttribute intrinsicPropertyCategory = new CategoryAttribute("Intrinsic Properties");
currentDescriptor = new IntrinsicPropertyDescriptor(def);
TypeDescriptor.AddAttributes(currentDescriptor, new Attribute[] { intrinsicPropertyCategory });

我还尝试在一个PropertyDescriptors的构造函数中使用TypeDescriptor.AddAttributes(),如下所示。但它也不起作用。

代码语言:javascript
复制
public IntrinsicPropertyDescriptor(IntrinsicPropertyDef propDef): base(propDef.Key, propDef.Attributes)
{
this._type = propDef.Type;
this._key = propDef.Key;
this._readOnly = propDef.ReadOnly;

CategoryAttribute intrinsicPropertyCategory = new CategoryAttribute("Intrinsic Properties");
TypeDescriptor.AddAttributes(this, new Attribute[] { intrinsicPropertyCategory });
}

我不想花时间去详述我为什么要做我正在做的事情。但在上面的示例中,IntrinsicPropertyDef是一个类,它定义了一个属性,包括名称、显示名称和类型。所以propDef.Attributes包含了DisplayNameAttribute。

IntrinsicPropertyDef可以与两个不同的自定义PropertyDescriptors IntrinsicPropertyDescriptor和InferedIntrinsicPropertyDescriptor一起显示。每个IntrinsicPropertyDescriptor都应该有一个类别属性“固有属性”,每个InferedIntrinsicPropertyDescriptor都应该有一个类别属性“推断出的固有属性”。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-05-05 19:40:19

我相信你可以直接重写Category

代码语言:javascript
复制
public override string Category { get {return "Foo";}}

对于其他场景;通常使用自定义PropertyDescriptor时,需要在构造函数中指定属性。您需要扩展Attribute[]参数以包括CategoryAttribute。如果您需要进行任何处理,您可以使用静态方法-未测试:

代码语言:javascript
复制
static Attribute[] AddCategory(Attribute[] attributes, string category) {
    Array.Resize(ref attributes, attributes.Length + 1);
    attributes[attributes.Length - 1] = new CategoryAttribute(category);
    return attributes;
}
public IntrinsicPropertyDescriptor(IntrinsicPropertyDef propDef)
     : base(propDef.Key, AddCategory(propDef.Attributes, "Foo"))
{...}

还要注意,对于要使用的PropertyDescriptor,系统必须找到它...解析规则为:

对于

  • PropertyGrid,反射提供的属性默认为实例的属性(如下所示),而实例的反射为:
  • ICustomTypeDescriptor is checked
  • otherwise它会检查实例的注册反射,或者type
  • otherwise TypeConverter is type
    • otherwise TypeDescriptionProvider

类型的

  • :为used

的反射检查注册的TypeDescriptionProvider

列表的处理类型:检查

  • IListSource并将其解析为列表(processing continues)
  • ITypedList is checked
  • otherwise,)检查非对象索引器的列表类型-即,如果找到public SomeType this[int index] {get;}
    • ,则按照定义的
    • 使用public SomeType this[int index] {get;}

代码语言:javascript
复制
- otherwise, if the list is not empty, the properties of the first instance (`list[0]`) are used, as defined above
- otherwise, metadata is not available

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/826437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档