New to Telerik UI for WinFormsStart a free 30-day trial

How to Specify Displayed Enum Values in RadPropertyGrid

Updated over 6 months ago

Environment

Product VersionProductAuthor
2022.1.222RadPropertyGrid for WinFormsDesislava Yordanova

Description

RadPropertyGrid activates a PropertyGridDropDownListEditor for enum properties. The drop down items correspond to the actual names of the enum values. Let's consider the following setup:

C#

public RadForm1()
{
    InitializeComponent();

    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
} 
    

The below screenshot illustrates how the items are presented when the editor is activated:

specify-displayed-enum-values-in-propertygrid 001

This article demonstrates a sample approach how to modify the displayed items in the drop down editor.

Solution

To change the string representation of a value inside RadPropertyGrid one should use a Type Converter. 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:

The achieved result is illustrated below:

specify-displayed-enum-values-in-propertygrid 002

C#

public RadForm1()
{
    InitializeComponent();

    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;
    }
}

[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
}

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);
    }
}      
    

See Also