How to: Supply Predefined Values for the String Property Editor Dynamically (WinForms) 如何:动态为字符串属性编辑器提供预定义值(WinForms)

This topic describes implementation of a custom Property Editor for a Windows Forms application. A custom Property Editor used to edit a business object's CultureCode (locale) property of the String type is implemented here. The dropdown list of the Property Editor's control will display the cultures installed in an end-user's Windows operating system.

本主题介绍 Windows 窗体应用程序的自定义属性编辑器的实现。此处实现了用于编辑 String 类型的业务对象的区域性代码(区域设置)属性的自定义属性编辑器。属性编辑器控件的下拉列表将显示安装在最终用户的 Windows 操作系统中的区域性。

 

Note 注意
You can also specify predefined values in the Model Editor using the IModelCommonMemberViewItem.PredefinedValues property. This approach is much simpler because it does not require extra coding, but you will be unable to update the values list dynamically in code in this instance.
您还可以使用 IModelCommonMemberViewItem.预定义值在模型编辑器中指定预定义值。此方法要简单得多,因为它不需要额外的编码,但在这种情况下,您将无法动态更新代码中的值列表。

 

The image below shows the resulting Property Editor:

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

CustomPropertyEditor

By default, XAF creates the StringPropertyEditor for String type properties. This Property Editor displays a dropdown list of items if the corresponding BOModel | <Class> | OwnMembers | <Member> node's RowCount property is set to 0 and the PredefinedValues property contains a predefined list values for the dropdown. However, if you do not know the values before run time it is necessary to implement a custom Property Editor.

默认情况下,XAF 为字符串类型属性创建 StringPropertyEditor。此属性编辑器显示项的下拉列表(如果相应的 BOModel |<Class>*自己的会员 |<Member>节点的 RowCount 属性设置为 0,预定义值属性包含下拉列表的预定义列表值。但是,如果在运行时之前不知道这些值,则需要实现自定义属性编辑器。

 

Since you are going to use a control supplied by DevExpress, implement a custom Property Editor by overriding the DXPropertyEditor class methods. You can inherit this class directly, or inherit one of its descendants (e.g., StringPropertyEditor). Note that your editor should be public. So, the following methods must be overridden.

  1. Override the CreateControlCore method to return the ComboBoxEdit control.
  2. Override the CreateRepositoryItem method to return RepositoryItemComboBox type item, because it is the appropriate type for the ComboBoxEdit control.
  3. Override the SetupRepositoryItem method to populate the control's dropdown list with items. We use the CultureInfo.GetCultures method to retrieve the cultures installed.

默认情况下,XAF 为字符串类型属性创建 StringPropertyEditor。此属性编辑器显示项的下拉列表(如果相应的 BOModel |<Class>*自己的会员 |<Member>节点的 RowCount 属性设置为 0,预定义值属性包含下拉列表的预定义列表值。但是,如果在运行时之前不知道这些值,则需要实现自定义属性编辑器。 

1.覆盖"创建控制核心"方法以返回组合框编辑控件。

2. 重写"创建存储库"项目方法以返回存储库项目ComboBox类型项,因为它是组合框编辑控件的合适类型。

3. 覆盖 Setup 存储库Item 方法,以用项填充控件的下拉列表。我们使用文化信息.Get文化方法检索已安装的区域性。

 

To specify that the implemented Property Editor can be used for the String type properties, the PropertyEditor attribute is applied:

为了指定实现的属性编辑器可用于 String 类型属性,将应用属性编辑器属性:

using System;
using System.Globalization;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Win.Editors;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.ExpressApp.Model;
//...
[PropertyEditor(typeof(String), false)]
public class CustomStringEditor : StringPropertyEditor {
    public CustomStringEditor(Type objectType, IModelMemberViewItem info)
        : base(objectType, info) {
    }
    protected override object CreateControlCore() {
        return new ComboBoxEdit();
    }
    protected override RepositoryItem CreateRepositoryItem() {
        return new RepositoryItemComboBox();
    }
    protected override void SetupRepositoryItem(
        DevExpress.XtraEditors.Repository.RepositoryItem item) {
        base.SetupRepositoryItem(item);
        foreach (CultureInfo culture in CultureInfo.GetCultures(
            CultureTypes.InstalledWin32Cultures)) {
            ((RepositoryItemComboBox)item).Items.Add(
                culture.EnglishName + "(" + culture.Name + ")");
        }
        ((RepositoryItemComboBox)item).TextEditStyle = TextEditStyles.DisableTextEditor;
    }
}

 

In this example, we apply the ModelDefaultAttribute attribute to use the implemented Property Editor for a business object's CultureCode property:

在此示例中,我们应用 ModelDefault属性,以使用业务对象的区域性代码属性实现的属性编辑器:

using DevExpress.ExpessApp.Model;
//...
[ModelDefault("PropertyEditorType", "Solution1.Module.Win.CustomStringEditor")]
public String CultureCode {
   get { return GetPropertyValue<String>(nameof(CultureCode)); }
   set { SetPropertyValue(nameof(CultureCode), value); }
}

Here, the ModelDefault attribute specifies the PropertyEditorType property of the Application Model's IModelMember node that defines the CultureCode property. Alternatively, you can do it via the Model Editor.

在这里,ModelDefault 属性指定应用程序模型的 IModelMember 节点的属性编辑器类型属性,该节点定义区域性代码属性。或者,您可以通过模型编辑器执行此操作。

 

Note 注意
You can see the code demonstrated here along with more examples on custom property editors in the Feature Center Demo installed with XAF.
您可以在使用 XAF 安装的功能中心演示中查看此处演示的代码以及有关自定义属性编辑器的更多示例。
posted @ 2020-01-07 21:18  code first life  阅读(235)  评论(0编辑  收藏  举报