Custom Editors

  • 3 minutes to read

This document describes how to create custom editors that you can use for standalone and in-place editing within container controls (for example, Data Grid). Custom editors should comply with the object model applied in the DXEditors Library. This object model is described in Editors Class Structure.

General Information

Follow the steps listed below to create a custom editor.

  • Create a custom editor class that implements specific behavior.

    To implement a completely new functionality, you can inherit from the BaseEdit class. If the editor you are creating has something in common with an editor shipped with the DXEditors Library, you can inherit from the appropriate editor. For information on data editors included in the DXEditors Library, see Editors Hierarchy and Included Components.

  • Create a custom helper class that holds an editor's settings and is responsible for the editor's in-place functionality within a container control.

    Each editor in the DXEditors Library has an associated Settings object. For instance, settings for a DateEdit control are encapsulated by the DateEditSettings class, while settings for an ButtonEdit editor are provided by the ButtonEditSettings class, etc. All settings objects are derived from the base BaseEditSettings class.

  • Register a custom editor and its settings.

    Each editor must be registered in an internal registration collection so that this editor can then be accessed by any container control (Data Grid, Bars, etc.). The registration code must be placed in a static constructor of the custom editor class and its settings class.

    NOTE

    Editors registration is not thread-safe and should be done in the main thread before the first use.

Example: Custom Text Edit

This example demonstrates how to create a custom editor that derives from the TextEdit class and introduces a new IsDefaultEditor Boolean dependency property. A custom editor is registered by the RegisterCustomEdit method.

using System;
using System.Windows;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Editors.Helpers;
using DevExpress.Xpf.Editors.Settings;

namespace CustomTextEditorExample {
    public class CustomTextEdit : TextEdit {

        public static readonly DependencyProperty IsDefaultEditorProperty;

        // The static constructor which calls the registration method.
        static CustomTextEdit() {
            IsDefaultEditorProperty = DependencyProperty.Register("IsDefaultEditor", typeof(bool), typeof(CustomTextEdit));

            CustomTextEditSettings.RegisterCustomEdit();
        }

        public CustomTextEdit() {
        }

        public bool IsDefaultEditor {
            get { return (bool)GetValue(IsDefaultEditorProperty); }
            set { SetValue(IsDefaultEditorProperty, value); }
        }
    }
    public class CustomTextEditSettings : TextEditSettings {

        public static readonly DependencyProperty IsDefaultEditorProperty;

        // The static constructor which calls the registration method.
        static CustomTextEditSettings() {
            IsDefaultEditorProperty = DependencyProperty.Register("IsDefaultEditor", typeof(bool), typeof(CustomTextEditSettings));

            RegisterCustomEdit();
        }

        // Registers the editor.
        public static void RegisterCustomEdit() {
            EditorSettingsProvider.Default.RegisterUserEditor(typeof(CustomTextEdit),
                typeof(CustomTextEditSettings),
                () => new CustomTextEdit(),
                () => new CustomTextEditSettings());
        }

        public bool IsDefaultEditor {
            get { return (bool)GetValue(IsDefaultEditorProperty); }
            set { SetValue(IsDefaultEditorProperty, value); }
        }

        protected override void AssignToEditCore(IBaseEdit edit) {
            base.AssignToEditCore(edit);
            CustomTextEdit editor = edit as CustomTextEdit;
            if (editor == null)
                return;
            SetValueFromSettings(IsDefaultEditorProperty, () => editor.IsDefaultEditor = IsDefaultEditor);
        }
    }
}

The following code shows how to assign a custom editor to an DXGrid column.

<dxg:GridColumn FieldName="ProductName">
    <dxg:GridColumn.EditSettings>
        <local:CustomTextEditSettings IsDefaultEditor="True"/>
    </dxg:GridColumn.EditSettings>
</dxg:GridColumn>