wpf-9325-controls-and-libraries-data-editors-implementation-details-custom-editors.md
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.
Follow the steps listed below to create a custom editor.
Create a custom editor class that implements specific behavior.
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.
Register a custom editor and its settings.
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>