DevExpress-Examples / winforms-property-grid-create-custom-editor

Create a custom cell editor and use it in a Property Grid.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WinForms Property Grid - Use a custom cell editor

This example demonstrates how to implement a UI Type Editor (FilteredFileNameEditor) and use it within the PropertyGridControl.

  1. Create a FilteredFileNameEditor class and implement the UITypeEditor interface:

    internal class FilteredFileNameEditor : UITypeEditor {
        private OpenFileDialog ofd = new OpenFileDialog();
        public override UITypeEditorEditStyle GetEditStyle(
         ITypeDescriptorContext context) {
            return UITypeEditorEditStyle.Modal;
        }
        public override object EditValue(
         ITypeDescriptorContext context,
         IServiceProvider provider,
         object value) {
            ofd.FileName = value.ToString();
            ofd.Filter = "Text File|*.txt|All Files|*.*";
            if (ofd.ShowDialog() == DialogResult.OK) {
                return ofd.FileName;
            }
            return base.EditValue(context, provider, value);
        }
    }
  2. Apply the System.ComponentModel.Editor attribute as follows:

    public class File {
        [System.ComponentModel.Editor(typeof(UIEditors.FilteredFileNameEditor),
          typeof(System.Drawing.Design.UITypeEditor))]
        public string Path { get; set; }
        public string Path2 { get; set; }
    }
  3. Assign a ButtonEdit to a cell as shown in the Assigning Editors to Editor Rows topic.

    private void Form1_Shown(object sender, EventArgs e) {
        RepositoryItemButtonEdit edit = new RepositoryItemButtonEdit();
        edit.ButtonClick += edit_ButtonClick;
        (this.propertyGridControl1.Rows[0] as CategoryRow).ChildRows["rowPath2"].Properties.RowEdit = edit;
    }
  4. Handle the Button Editor's ButtonClick event.

    void edit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {
        this.openFileDialog1.ShowDialog();
    }

Files to Review

About

Create a custom cell editor and use it in a Property Grid.

License:Other


Languages

Language:Visual Basic .NET 50.7%Language:C# 49.3%