Custom Editors¶
Register Custom Editors for Use in Container Controls¶
A standard approach to specifying in-place editors in container controls is to use the EditorProperties property provided by these container controls:
GridColumn.EditorPropertiesTreeListColumn.EditorPropertiesTreeViewControl.EditorPropertiesPropertyGridRow.EditorPropertiesToolbarEditorItem.EditorProperties
For example, the following code assigns a ButtonEditor control to a grid column. The code sets the GridColumn.EditorProperties property to a ButtonEditorProperties object. As a result, the grid control will automatically create an in-place ButtonEditor control based on the specified ButtonEditorProperties object when a cell edit operation starts in this column.
xmlns:mxdg="https://schemas.eremexcontrols.net/avalonia/datagrid"
xmlns:mxe="https://schemas.eremexcontrols.net/avalonia/editors"
<mxdg:DataGridControl.Columns>
<mxdg:GridColumn Header="Name" FieldName="Name">
<mxdg:GridColumn.EditorProperties>
<mxe:ButtonEditorProperties>
<mxe:ButtonEditorProperties.Buttons>
<mxe:ButtonSettings Content="Clear"
Command="{Binding
$parent[mxdg:CellControl].DataControl.DataContext.ClearValueCommand}"/>
</mxe:ButtonEditorProperties.Buttons>
</mxe:ButtonEditorProperties>
</mxdg:GridColumn.EditorProperties>
</mxdg:GridColumn>
</mxdg:DataGridControl.Columns>
To allow custom Eremex editor descendants to be assigned this way to cells in container controls, do the following:
-
Create a descendant of the Eremex editor control
Tip
All Eremex editors are
BaseEditordescendants. -
Create a descendant of the corresponding
...Propertiesclass. For instance, if your custom editor is derived from theButtonEditorcontrol, derive your...Propertiesclass fromButtonEditorProperties.Tip
All
...Propertiesclasses of Eremex editors are descendants of theBaseEditorPropertiesclass. -
Add custom editor registration code to the static constructor of your
...Propertiesclass. Use theEditorPropertiesProvider.Default.RegisterEditormethod to register the editor.
The EditorPropertiesProvider.Default.RegisterEditor method has the following signature:
void RegisterEditor(Type editor, Type editorProperties, Func<IBaseEditor> createEditorFunc, Func<BaseEditorProperties> createEditorPropertiesFunc)
editor— The type of a custom editor control.editorProperties— The type of a corresponding...Propertiesclass.createEditorFunc— A custom function invoked when a custom editor control is created.createEditorPropertiesFunc— A custom function invoked when a custom...Propertiesobject is created.
The following code creates a custom TextEditor control. It overrides the display format used to format the editor's values. The TextEditorWithCustomFormatProperties class contains a static constructor which registers the editor.
using Eremex.AvaloniaUI.Controls.Editors;
public class TextEditorWithCustomFormat : TextEditor
{
protected override Type StyleKeyOverride => typeof(TextEditor);
}
public class TextEditorWithCustomFormatProperties : TextEditorProperties
{
public static string Formatter = $"Header_{{0}}";
public TextEditorWithCustomFormatProperties()
{
DisplayFormatString = Formatter;
}
static TextEditorWithCustomFormatProperties()
{
RegisterEditor();
}
static void RegisterEditor()
{
EditorPropertiesProvider.Default.RegisterEditor(typeof(TextEditorWithCustomFormat), typeof(TextEditorWithCustomFormatProperties),
() => new TextEditorWithCustomFormat(), () => new TextEditorWithCustomFormatProperties());
}
}