编辑PropertyGrid中枚举成员的显示名称

Jon*_*son 14 .net c#

我有一个属性网格,我用它为用户能够为我的应用程序中使用的任何插件配置对象.我希望能够告诉开发人员编写插件来为其成员使用ComponentModel属性,如下所示:

[CategoryAttribute("On Screen Display Settings"),
 DescriptionAttribute("Whether or not to show the session timer."),
 DisplayName("Show Session Timer")]
 public bool ShowTimer
 {
    get;
    set;
 }
Run Code Online (Sandbox Code Playgroud)

这非常有效.现在我希望枚举的成员也能够被编辑.即

public enum Resolution_ : byte
{
    DCIF,
    CIF,
    QCIF,
    [DisplayName("4CIF")]
    CIF4,
    [DisplayName("2CIF")]
    CIF2
}
Run Code Online (Sandbox Code Playgroud)

因此它们显示在PropertyGrid的列表中,如下所示:

 DCIF
 CIF
 QCIF
 CIF4
 CIF2
Run Code Online (Sandbox Code Playgroud)

以及他们可能拥有的任何描述和显示名称.

似乎我只能用属性,事件和方法来做这件事.有谁知道如何为枚举做这个?

Lar*_*ech 17

您必须创建一个EnumConverter类并使用TypeConverter属性装饰您的 属性才能执行此操作.

请参阅本使用PropertyGrid中的.NET,这是一个有趣的例子:

想象一下,你想要列表中的两个以上的项目.布尔类型是不够的; 您需要为枚举中的每个元素设置Description属性.

enum DrinkDoses {
  [Description("Half of litre")]
  litre,
  [Description("One litre")]
  oneLitre,
  [Description("Two litres")]
  twoLitre,
  [Description("Three litres")]
  threeLitres,
  [Description("Four litres")]
  fourLitres,
  [Description("Death dose, five litres")]
  fiveLitres
}
Run Code Online (Sandbox Code Playgroud)

在另一个类中,您需要使用类型EnumConverter.

class DrinkDosesConverter : EnumConverter {
  private Type enumType;

  public DrinkDosesConverter(Type type) : base(type) {
    enumType = type;
  }

  public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) {
    return destType == typeof(string);
  }

  public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
                                   object value, Type destType) {
    FieldInfo fi = enumType.GetField(Enum.GetName(enumType, value));
    DescriptionAttribute dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, 
                                typeof(DescriptionAttribute)); 
    if (dna != null)
      return dna.Description;
    else
      return value.ToString();
  }

  public override bool CanConvertFrom(ITypeDescriptorContext context, Type srcType) {
    return srcType == typeof(string);
  } 

  public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture,
                                     object value) {
    foreach (FieldInfo fi in enumType.GetFields()) {
      DescriptionAttribute dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, 
                                  typeof(DescriptionAttribute)); 
      if ((dna != null) && ((string)value == dna.Description))
        return Enum.Parse(enumType, fi.Name);
    }
    return Enum.Parse(enumType, (string)value);
  }
}
Run Code Online (Sandbox Code Playgroud)

第三,您需要设置属性TypeConverter以显示属性.

class DrinkerDoses {
  DrinkDoses doses;
  [DisplayName("Doses")]
  [Description("Drinker doses")]
  [Category("Alcoholics drinking")]
  [TypeConverter(typeof(DrinkDosesConverter))]
  public DrinkDoses Doses {
    get { return doses; }
    set { doses = value; }
  }
  int dataInt; 
  public int DataInt {
    get { return dataInt; }
    set { dataInt = value; }
  }
}
Run Code Online (Sandbox Code Playgroud)