Creating new Field Definition Classes
Creating a field definition class for a new input control is simply a case of descending a new class from
BaseFieldDefinition and overriding the
BuildInputControl() method.
As an example, we'll see how to implement a fictional numeric integer input control, called IntegerEditor.
First, create a new class descended from BaseFieldDefinition:
public class IntegerEditorFieldDefinition : BaseFieldDefinition {
}
Our fictional control has two properties we want to be able to customize -
MinValue and
MaxValue . To allow this, we'll add similar properties to our new class:
public int MinValue { get; set; }
public int MaxValue { get; set; }
Next, we need to override BuildInputControl to create an instance of IntegerEditor. The method needs to:
- Create an instance of IntegerEditor
- Set up a binding for the IntegerEditor.Value property
- Optionally set up a Binding for the IsEnabled property
- Set the InputControl and ContainerControl properties
protected override void BuildInputControl() {
IntegerEditor editor = new IntegerEditor() {
MinValue = this.MinValue,
MaxValue = this.MaxValue
};
Binding valueBinding = new Binding(PropertyName);
BindingOperations.SetBinding(editor, IntegerEditor.ValueProperty, valueBinding);
if (!String.IsNullOrWhiteSpace(EnabledPropertyName))
{
Binding enabledBinding = new Binding(EnabledPropertyName);
if (InvertEnabledState)
{
// InverseBooleanConverter is in the AutoForm namespace
enabledBinding.Converter = new InverseBooleanConverter();
}
BindingOperations.SetBinding(textBox, TextBox.IsEnabledProperty, enabledBinding);
}
ContainerControl = InputControl = textBox;
}
All that remains is to register the new field definition:
FieldDefinitionRegistry.Register<int, IntegerEditorFieldDefinition>();