How to: Implement a Property Editor Using a DevExpress WinForms Control 如何:使用 DevExpress WinForms 控件实现属性编辑器

This topic demonstrates how to implement a Property Editor with a custom mask. This Property Editor will use the CalcEdit editor from the XtraEditors library. A currency mask will be set for this editor.

本主题演示如何使用自定义掩码实现属性编辑器。此属性编辑器将使用 XtraEditors 库中的 CalcEdit 编辑器。将为此编辑器设置货币掩码。

Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E232
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E232

.

The image below shows the resulting Property Editor:

下图显示了生成的属性编辑器:

CalcEditPropertyEditor

 

Since we are going to use an editor from the XtraEditors library, our Property Editor should be inherited from the DXPropertyEditor class. This class supports the editor's capability to be used for inplace editing. It exposes extra methods for this. Note that your editor should be public.

由于我们将使用来自 XtraEditor 库的编辑器,因此我们的属性编辑器应从 DXPropertyEditor 类继承。此类支持编辑器用于就地编辑的功能。它为此公开了额外的方法。请注意,您的编辑器应该是公开的。

using DevExpress.ExpressApp.Win.Editors;
//...
public class MyDecimalCalcEditPropertyEditor : DXPropertyEditor {

}

 

When implementing a Property Editor, you should apply the PropertyEditor attribute to it. This attribute represents an indicator for the Application Model loader. The classes that use this attribute can be set to display properties of the type specified by the attribute's parameter.

实现属性编辑器时,应将属性编辑器属性应用于它。此属性表示应用程序模型加载器的指示器。可以将使用此属性的类设置为显示属性参数指定的类型的属性。

using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Win.Editors;
//...
[PropertyEditor(typeof(decimal), true)]
public class MyDecimalCalcEditPropertyEditor : DXPropertyEditor {

}

 Now, our Property Editor will be available within the Property Editor types that can display decimal properties. To set a Property Editor to be automatically used for all properties of a specified data type, pass true as the second attribute parameter.

现在,我们的属性编辑器将在属性编辑器类型中可用,该类型可以显示十进制属性。要将属性编辑器设置为自动用于指定数据类型的所有属性,将 true 作为第二个属性参数传递。

 

The DXPropertyEditor class sets the control's EditValue property as a binding property. However, the CalcEdit control converts the edit value to the Decimal type. The editor's decimal value can be accessed via the Value property. So, we can set the Value property as a binding property.

DXPropertyEditor 类将控件的 EditValue 属性设置为绑定属性。但是,CalcEdit 控件将编辑值转换为十进制类型。可通过 Value 属性访问编辑器的十进制值。因此,我们可以将 Value 属性设置为绑定属性。

using System;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Model;
//...
[PropertyEditor(typeof(decimal), true)]
public class MyDecimalCalcEditPropertyEditor : DXPropertyEditor {
   public MyDecimalCalcEditPropertyEditor(Type objectType, IModelMemberViewItem model) 
      : base(objectType, model) {
      this.ControlBindingProperty = "Value";
   }
}

 

To specify the CalcEdit editor as a control that will be used to display the Property Editor's property, override the CreateControlCore method:

要将 CalcEdit 编辑器指定为将用于显示属性编辑器属性的控件,请重写"创建控制核心"方法:

using DevExpress.XtraEditors;
//...
[PropertyEditor(typeof(decimal), true)]
public class MyDecimalCalcEditPropertyEditor : DXPropertyEditor {
   protected override object CreateControlCore() {
      return new CalcEdit();
   }
//...
}

 

To specify the required settings for our Property Editor, we need to override the SetupRepositoryItem method (see DXPropertyEditor). This method's item parameter specifies the default repository item created for the CalcEdit editor (see CalcEdit.Properties).

要指定属性编辑器所需的设置,我们需要重写安装程序存储库项目方法(请参阅 DXPropertyEditor)。此方法的项参数指定为 CalcEdit 编辑器创建的默认存储库项(请参阅 CalcEdit.Properties)。

using DevExpress.XtraEditors.Repository;
//...
[PropertyEditor(typeof(decimal), true)]
public class MyDecimalCalcEditPropertyEditor : DXPropertyEditor {
   protected override void SetupRepositoryItem(RepositoryItem item) {
      base.SetupRepositoryItem(item);
         ((RepositoryItemCalcEdit)item).Mask.EditMask = "C";
         ((RepositoryItemCalcEdit)item).Mask.UseMaskAsDisplayFormat = true;
   }
   //...
}

 In the code above, the Currency mask is set for the editor. The same mask is set to be used as a display format.

在上面的代码中,为编辑器设置了货币掩码。相同的蒙版设置为用作显示格式。

 

To use our Property Editor inplace, for example, in an editable GridListEditor, we should override the CreateRepositoryItem method, and return the required repository item.

例如,要在可编辑的 GridListEditor 中,使用我们的属性编辑器,应重写 Create 存储库项目方法,并返回所需的存储库项。

[PropertyEditor(typeof(decimal), true)]
public class MyDecimalCalcEditPropertyEditor : DXPropertyEditor {
   protected override RepositoryItem CreateRepositoryItem() {
      return new RepositoryItemCalcEdit();
   }
   //...
}

 

Note 注意
Overriding the CreateRepositoryItem method is optional. This is only required if you are going to use the Property Editor in a cell of an editable List Editor. Note that a new PropertyEditor object is created to initialize the GridColumn.ColumnEdit property and then this object is immediately disposed. So, the custom PropertyEditor descendant is an incorrect place for event handlers. Instead, handle events or override corresponding protected virtual methods in your RepositoryItem class, introduce necessary properties and initialize them in the CreateRepositoryItem method of your Property Editor.
重写创建存储库项目方法是可选的。仅当要在可编辑列表编辑器的单元格中使用属性编辑器时,才需要这样做。请注意,将创建新的属性编辑器对象以初始化 GridColumn.ColumnEdit 属性,然后立即释放此对象。因此,自定义属性编辑器后代是事件处理程序的不正确位置。相反,在 RepositoryItem 类中处理事件或重写相应的受保护虚拟方法,引入必要的属性,并在属性编辑器的 Create 存储库项方法中初始化它们。

To see our Property Editor in use, set it for a decimal property in the Model Editor invoked for the Windows Forms application project. To do this, use the PropertyEditorType property of the BOModel | <Class> | OwnMembers | <Member> or the Views | <DetailView> | Items | <PropertyEditor> node.

要查看正在使用的属性编辑器,请为为 Windows 窗体应用程序项目调用的模型编辑器中的十进制属性设置该属性。为此,请使用 BOModel 的属性编辑器类型属性 |<Class>*自己的会员 |<Member>或视图 |<DetailView>*项目 |<PropertyEditor>节点。

posted @ 2020-01-08 12:31  code first life  阅读(452)  评论(1编辑  收藏  举报