商信互联
本主题描述了自定义ASP.NET Web属性编辑器的实现,该编辑器将用于编辑String类型的业务对象的CultureCode(语言环境)属性。属性编辑器控件的下拉列表将显示CultureInfo.GetCultures方法返回的区域性。
您还可以使用IModelCommonMemberViewItem.PredefinedValues属性在模型编辑器中指定预定义值。这种方法非常简单,因为它不需要额外的编码,但是在这种情况下,您将无法在代码中动态更新值列表。
下图显示了生成的属性编辑器:
要通过继承ASPxPropertyEditor类来实现属性编辑器,请重写以下方法(请参阅ASPxPropertyEditor):
要指定可以将实现的属性编辑器用于String类型的属性,将应用PropertyEditor属性(请参见PropertyEditorAttribute):
using System;
using System.Globalization;
using System.Web.UI.WebControls;
using DevExpress.Web;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Web;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Web.Editors.ASPx;
using DevExpress.ExpressApp.Model;
//...
[PropertyEditor(typeof(String), false)]
public class CustomStringEditor : ASPxPropertyEditor {
ASPxComboBox dropDownControl = null;
public CustomStringEditor(
Type objectType, IModelMemberViewItem info) : base(objectType, info) { }
protected override void SetupControl(WebControl control) {
if(ViewEditMode == ViewEditMode.Edit) {
foreach(CultureInfo culture in CultureInfo.GetCultures(
CultureTypes.InstalledWin32Cultures)) {
((ASPxComboBox)control).Items.Add(culture.EnglishName "(" culture.Name ")");
}
}
}
protected override WebControl CreateEditModeControlCore() {
dropDownControl = RenderHelper.CreateASPxComboBox();
dropDownControl.ValueChanged = EditValueChangedHandler;
return dropDownControl;
}
public override void BreakLinksToControl(bool unwireEventsOnly) {
if(dropDownControl != null) {
dropDownControl.ValueChanged -= new EventHandler(EditValueChangedHandler);
}
base.BreakLinksToControl(unwireEventsOnly);
}
}
Imports System
Imports System.Globalization
Imports System.Web.UI.WebControls
Imports DevExpress.Web
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Web
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Web.Editors.ASPx
Imports DevExpress.ExpressApp.Model
'...
<PropertyEditor(GetType(String), False)> _
Public Class CustomStringEditor
Inherits ASPxPropertyEditor
Private dropDownControl As ASPxComboBox = Nothing
Public Sub New(ByVal objectType As Type, ByVal info As IModelMemberViewItem)
MyBase.New(objectType, info)
End Sub
Protected Overrides Sub SetupControl(ByVal control As WebControl)
If ViewEditMode = ViewEditMode.Edit Then
For Each culture As CultureInfo In CultureInfo.GetCultures( _
CultureTypes.InstalledWin32Cultures)
CType(control, ASPxComboBox).Items.Add(culture.EnglishName & "(" & culture.Name & ")")
Next culture
End If
End Sub
Protected Overrides Function CreateEditModeControlCore() As WebControl
dropDownControl = RenderHelper.CreateASPxComboBox()
AddHandler dropDownControl.ValueChanged, AddressOf EditValueChangedHandler
Return dropDownControl
End Function
Public Overrides Sub BreakLinksToControl(ByVal unwireEventsOnly As Boolean)
If dropDownControl IsNot Nothing Then
RemoveHandler dropDownControl.ValueChanged, AddressOf EditValueChangedHandler
End If
MyBase.BreakLinksToControl(unwireEventsOnly)
End Sub
End Class
应用ModelDefaultAttribute属性以将实现的属性编辑器用于业务对象的CultureCode属性:
using DevExpress.ExpressApp.Model;
//...
[ModelDefault("PropertyEditorType", "Solution1.Module.Web.CustomStringEditor")]
public String CultureCode {
get { return GetPropertyValue<String>(nameof(CultureCode)); }
set { SetPropertyValue(nameof(CultureCode), value); }
}
Imports DevExpress.ExpressApp.Model
'...
<ModelDefault("PropertyEditorType", "Solution1.Module.Web.CustomStringEditor")> _
Public Property CultureCode() As String
Get
Return GetPropertyValue(Of String)(NameOf(CultureCode))
End Get
Set(ByVal value As String)
SetPropertyValue(NameOf(CultureCode), value)
End Set
End Property
在这里,Custom属性指定了应用程序模型的IModelMember节点的PropertyEditorType属性,该属性定义了CultureCode属性。或者,您可以使用“模型编辑器”来执行此操作。
您可以在默认情况下,或位于功能中心的%PUBLIC%\ Documents \ DevExpress演示19.2 \ Components \ eXpressApp Framework \ FeatureCenter文件夹中的功能中心演示中,查看此处演示的代码以及自定义属性编辑器上的更多示例。在线演示。