Declined
Last Updated: 27 Nov 2017 10:15 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 14 Aug 2017 07:30
Category: PropertyGrid
Type: Bug Report
3
FIX. RadPropertyGrid - Description attribute of the enum is not respected
Please run the attached sample project and refer to the attached screenshots. RadPropertyGrid doesn't respect the Description attribute.

 this.radPropertyGrid1.SelectedObject = new Item(123,"Item", ExposureMode.FullAuto);

public class Item
{
    public int Id { get; set; }

    public string Name { get; set; }

    public ExposureMode Mode { get; set; }

    public Item(int id, string name, ExposureMode mode)
    {
        this.Id = id;
        this.Name = name;
        this.Mode = mode;
    }
}

public enum ExposureMode
{
    [Description("Full Auto")]
    FullAuto,
    [Description("Auto Filter, Fixed Exposure")]
    AutoFilFixedExp,
    [Description("Fixed Filter, Auto Exposure")]
    FixedFilAutoExp,
    [Description("Fixed Filter, Fixed Exposure")]
    FullFixed
}
1 comment
ADMIN
Ivan Petrov
Posted on: 15 Nov 2017 13:57
To change the string representation of a value inside RadPropertyGrid one should use a TypeConverter. This converter provides a way for a value of a property to be represented as a string and a string value (user input) to be converted to a property value of the desired type. Here is a generic EnumTypeConverter that will work with any Enum that has its values decorated with a Description attribute:

public class EnumDescriptionTypeConverter<T> : EnumConverter
{
    private Dictionary<string, T> lookupDescription;
    private Dictionary<T, string> lookupValue;

    public EnumDescriptionTypeConverter(Type type)
        : base(type)
    {
        this.lookupDescription = new Dictionary<string, T>();
        this.lookupValue = new Dictionary<T, string>();

        string[] names = Enum.GetNames(type);

        foreach (string name in names)
        {
            if (!string.IsNullOrEmpty(name))
            {
                FieldInfo field = type.GetField(name);

                if (field != null)
                {
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

                    if (attr != null)
                    {
                        T value = (T)Enum.Parse(type, name);
                        this.lookupDescription[attr.Description] = value;
                        this.lookupValue[value] = attr.Description;
                    }
                }
            }
        }
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            T mode = (T)value;

            if (this.lookupValue.ContainsKey(mode))
            {
                return this.lookupValue[mode];
            }
        }

        return base.ConvertTo(context, culture, value, destinationType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        string description = value as string;

        if (description != null)
        {
            return this.lookupDescription[description];
        }

        return base.ConvertFrom(context, culture, value);
    }
}

To use this converter decorate your enum with it and decorate its values with the Description attribute:

[TypeConverter(typeof(EnumDescriptionTypeConverter<ExposureMode>))]
public enum ExposureMode
{
    [Description("Full Auto")]
    FullAuto,
    [Description("Auto Filter, Fixed Exposure")]
    AutoFilFixedExp,
    [Description("Fixed Filter, Auto Exposure")]
    FixedFilAutoExp,
    [Description("Fixed Filter, Fixed Exposure")]
    FullFixed
}