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

FlagEnumEditor in RadPropertyGrid

Updated over 6 months ago

Environment

Product VersionProductAuthor
2019.2.917RadPropertyGrid for WinFormsDesislava Yordanova

Description

This tutorial demonstrates how to implement a custom editor in RadPropertyGrid in order to simulate the behavior of a FlagEnumEditor. It enables the user to store any combination of the values that are defined in the enumerator list:

flagenumeditor-in-radpropertygrid001

Solution

Create a derivative of BaseInputEditor and override its CreateEditorElement method where you will specify that a RadCheckedDropDownListElement will be used. The DataType property should indicate the type of the editor value. The Value property of the editor needs to return a valid value from the RadCheckedDropDownListElement back to the property grid item. It also has be able to parse the value coming from the item to the editor's element.

Last, but not least, the default editor for the Permissions item should be replaced with the custom one in the EditorRequired event.

FlagEnumEditor's implementation

C#
public RadForm1()
{
    InitializeComponent();
    this.radPropertyGrid1.EditorRequired += radPropertyGrid1_EditorRequired;
    PropertyStoreItem item1 = new PropertyStoreItem(typeof(string), "User Name", "Nancy Davolio");
    PropertyStoreItem item2 = new PropertyStoreItem(typeof(string), "Permissions", "Read;Write;");

    RadPropertyStore store = new RadPropertyStore();
    store.Add(item1);
    store.Add(item2);
    this.radPropertyGrid1.SelectedObject = store;
}

private void radPropertyGrid1_EditorRequired(object sender, PropertyGridEditorRequiredEventArgs e)
{
    if (e.Item.Label == "Permissions")
    {
        FlagEnumEditor editor = new FlagEnumEditor();
        RadCheckedDropDownListElement element = editor.EditorElement as RadCheckedDropDownListElement;
        element.DataSource = Enum.GetValues(typeof(Permissions));
        e.Editor = editor;
    }
}

public enum Permissions
{
    Read = 1,
    Write = 2,
    Execute = 4
}

public class FlagEnumEditor : BaseInputEditor
{
    public override Type DataType
    {
        get
        {
            return typeof(string);
        }
    }

    protected override RadElement CreateEditorElement()
    {
        return new RadCheckedDropDownListElement();
    }

    public override object Value
    {
        get
        {
            RadCheckedDropDownListElement element = this.EditorElement as RadCheckedDropDownListElement;
            return element.Text;
        }
        set
        {
            RadCheckedDropDownListElement element = this.EditorElement as RadCheckedDropDownListElement;
            element.Text = value + "";
        }
    }
}
        

See Also