expressappframework-112679-ui-construction-view-items-and-property-editors-property-editors-implement-a-property-editor-based-on-a-custom-control-winforms.md
This topic explains how to implement a Property Editor for WinForms applications. For demo purposes, the integer Property Editor based on the NumericUpDown control is implemented in this example.
Note
Follow these steps to implement a WinForms Property Editor.
The following code demonstrates CustomIntegerEditor class implementation based on the steps above.
using System.Windows.Forms;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Win.Editors;
using DevExpress.XtraEditors.Repository;
// ...
[PropertyEditor(typeof(Int32), false)]
public class CustomIntegerEditor : PropertyEditor, IInplaceEditSupport {
private NumericUpDown control = null;
protected override void ReadValueCore() {
if(control != null) {
if(CurrentObject != null) {
control.ReadOnly = false;
control.Value = (int)PropertyValue;
}
else {
control.ReadOnly = true;
control.Value = 0;
}
}
}
private void control_ValueChanged(object sender, EventArgs e) {
if(!IsValueReading) {
OnControlValueChanged();
WriteValueCore();
}
}
protected override object CreateControlCore() {
control = new NumericUpDown();
control.Minimum = 0;
control.Maximum = 5;
control.ValueChanged += control_ValueChanged;
return control;
}
protected override void OnControlCreated() {
base.OnControlCreated();
ReadValue();
}
public CustomIntegerEditor(Type objectType, IModelMemberViewItem info)
: base(objectType, info) {
}
protected override void Dispose(bool disposing) {
if(control != null) {
control.ValueChanged -= control_ValueChanged;
control = null;
}
base.Dispose(disposing);
}
RepositoryItem IInplaceEditSupport.CreateRepositoryItem() {
RepositoryItemSpinEdit item = new RepositoryItemSpinEdit();
item.MinValue = 0;
item.MaxValue = 5;
item.Mask.EditMask = "0";
return item;
}
protected override object GetControlValueCore() {
if(control != null) {
return (int)control.Value;
}
return null;
}
}
You can also implement IComplexViewItem in this Property Editor. This interface allows the editor to access the XafApplication instance and use an Object Space to load data from an application database.
To display a particular property using the CustomIntegerEditor Property Editor, customize the Application Model. Invoke the Model Editor for the WinForms application project and navigate to the required BOModel | Class | OwnMembers | Member node. Set the node’s IModelCommonMemberViewItem.PropertyEditorType property to CustomIntegerEditor. After this, the property specified by the Member node will be displayed by the CustomIntegerEditor in all Views. To use a CustomIntegerEditor Property Editor in a specific Detail View only, use the PropertyEditorType property of the Views | <DetailView> | Items | <PropertyEditor> node instead.
Note
You may need to implement the IAppearanceFormat interface and manually apply the IAppearanceFormat.BackColor, IAppearanceFormat.FontColor and IAppearanceFormat.FontStyle settings of the Conditional Appearance Module to the created control.
See Also