본문 바로가기
.NET

C# PropertyGrid에서 enum을 이용한 숫자 표시 방법(Enum Description 사용 방법)

by leo21c 2022. 4. 8.
SMALL

C#에서 PropertyGrid에 enum 변수를 model에 추가하면 간단하게 표시가 된다.

그런데 enum을 통해 표시할 것이 숫자라면 간단하지가 않다.

enum을 통해 표시할 내용

보통 enum을 아래와 같이 만든다.

public enum NUM_LIST : byte
{
    NONE = 0,
    _1 = 1,
    _2 = 2,
    _5 = 5,
    _6 = 6,
    _9 = 9,
    _10 = 10,
}

이렇게 추가를 하면 위의 이미지처럼 표시가 되지 않고 숫자가 _1, _2, ... 이렇게 표시가 된다.

enum의 key에 숫자를 사용할 수 없다.

그래서 _1, _2, 이렇게 넣은 것이다.

 

이때 사용하는 방법이 Description attribute이다.

이것을 사용해서 다시 만들면 아래와 같이 만들 수 있다.

public enum NUM_LIST : byte
{
    [Description("NONE")]
    NONE = 0,
    [Description("1")]
    _1 = 1,
    [Description("2")]
    _2 = 2,
    [Description("5")]
    _5 = 5,
    [Description("6")]
    _6 = 6,
    [Description("9")]
    _9 = 9,
    [Description("10")]
    _10 = 10,
}

위와 같이 만들어도 자동으로 표시가 되지 않는다.

우리가 PropertyGrid model class를 만들 때 아래와 같이 선언을 하고 TypeConveter를 추가해야 한다.

private NUM_LIST num_list;

[DisplayName("NUM_LIST items")]
[Description("enum NUM_LIST display")]
[Category("TEST")]
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
[DefaultValue(1)]
[Browsable(true)]
public NUM_LIST Num_List
{
    get => this.num_list;
    set
    {
        this.num_list = value;
        this.NotifyPropertyChanged(nameof(Num_List));
    }
}

위에 [TypeConverter(typeof(EnumDescriptionTypeConverter))]를 추가했다.

 

<참고> Edit the display name of enumeration members in a PropertyGrid

https://stackoverflow.com/questions/7422685/edit-the-display-name-of-enumeration-members-in-a-propertygrid

 

Edit the display name of enumeration members in a PropertyGrid

I have a property grid that I am using for users to be able to configure objects for any plugin that is written to be used in my application. I would like to be able to tell developers writing plug...

stackoverflow.com

위 참고 사이트에서 처리한 것과 같이 TypeConverter를 하나 만들어 사용한다.

이 클래스를 하나 만들어 놓으면 EnumDescription을 사용할 때 유용할 것이다.

class EnumDescriptionTypeConverter : EnumConverter {
  private Type enumType;

  public EnumDescriptionTypeConverter(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);
  }
}

이렇게 enum의 Description attribute를 이용하면 enum에서 어떤 key를 사용해도 원하는 string으로 표시를 할 수가 있다.

LIST