[英]Add a category attribute to a PropertyDescriptor
我有一組自定義PropertyDescriptor,我也想添加類別,以便它們在PropertyGrid中以更有條理的方式顯示。 我希望每種類型的PropertyDescriptor都進入特定的類別。
我嘗試使用TypeDescriptor.AddAttributes()將屬性添加到現有PropertyDescriptor,但不添加category屬性。
CategoryAttribute intrinsicPropertyCategory = new CategoryAttribute("Intrinsic Properties");
currentDescriptor = new IntrinsicPropertyDescriptor(def);
TypeDescriptor.AddAttributes(currentDescriptor, new Attribute[] { intrinsicPropertyCategory });
我也嘗試在我的一個PropertyDescriptors的構造函數中使用TypeDescriptor.AddAttributes(),如下所示。 但它也不起作用。
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是一個定義屬性的類,包括Name,Display Name和Type。 所以propDef.Attributes包含DisplayNameAttribute。
可以使用兩個不同的自定義PropertyDescriptors IntrinsicPropertyDescriptor和InferedIntrinsicPropertyDescriptor顯示IntrinsicPropertyDef。 每個IntrinsicPropertyDescriptor都應具有類別屬性“內在屬性”,並且每個InferedIntrinsicPropertyDescriptor都應具有類別屬性“推斷的內在屬性”。
我相信你可以覆蓋Category
:
public override string Category { get {return "Foo";}}
對於其他場景; 通常使用自定義PropertyDescriptor
,您可以在構造函數中指定屬性。 您需要擴展Attribute[]
參數以包含CategoryAttribute
。 如果需要進行任何處理,可以使用靜態方法 - 未經測試:
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
, TypeConverter
提供屬性,默認為實例的屬性(如下) ICustomTypeDescriptor
TypeDescriptionProvider
TypeDescriptionProvider
IListSource
並將其解析為列表(繼續處理) ITypedList
public SomeType this[int index] {get;}
SomeType
類型的屬性,如上所述 list[0]
)的屬性,如上所述
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.