商信互联
本主题说明如何为ASP.NET应用程序实现属性编辑器。为了演示起见,在此示例中实现了一个整数属性编辑器。它在“编辑”模式下使用DropDownList控件,在“视图”模式下使用Label控件。
您可以在XAF随附的FeatureCenter演示中看到此处实现的代码。默认情况下,此演示位于%PUBLIC%\ Documents \ DevExpress演示19.2 \ Components \ eXpressApp Framework \ FeatureCenter文件夹中。
使用此示例不能集成使用ClientScriptManager.RegisterStartupScript方法的ASP.NET控件。如果需要集成这样的控件,请随时与我们的支持团队联系。
请按照以下步骤来实现ASP.NET属性编辑器。
下面的代码根据上面列出的步骤演示CustomIntegerEditor类的实现。
using System;
using System.Drawing;
using System.Web.UI.WebControls;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Web.Editors;
//...
[PropertyEditor(typeof(Int32), false)]
public class CustomIntegerEditor : WebPropertyEditor {
public CustomIntegerEditor(Type objectType, IModelMemberViewItem info) : base(objectType, info) { }
protected override WebControl CreateViewModeControlCore() {
Label control = new Label();
control.ID = "editor";
return control;
}
protected override WebControl CreateEditModeControlCore() {
DropDownList control = new DropDownList();
control.ID = "editor";
control.Items.Add("0");
control.Items.Add("1");
control.Items.Add("2");
control.Items.Add("3");
control.Items.Add("4");
control.Items.Add("5");
control.SelectedIndexChanged = control_SelectedIndexChanged;
return control;
}
void control_SelectedIndexChanged(object sender, EventArgs e) {
EditValueChangedHandler(sender, e);
}
protected override object GetControlValueCore() {
int result = 0;
if(int.TryParse(((DropDownList)Editor).SelectedValue, out result)) {
return result;
}
return 0;
}
protected override void ReadEditModeValueCore() {
((DropDownList)Editor).SelectedValue = ((int)PropertyValue).ToString();
}
protected override void ReadViewModeValueCore() {
((Label)InplaceViewModeEditor).Text = ((int)PropertyValue).ToString();
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Web.UI.WebControls
Imports DevExpress.ExpressApp.Model
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Web.Editors
'...
<PropertyEditor(GetType(Int32), False)> _
Public Class CustomIntegerEditor
Inherits WebPropertyEditor
Public Sub New(ByVal objectType As Type, ByVal info As IModelMemberViewItem)
MyBase.New(objectType, info)
End Sub
Protected Overrides Function CreateViewModeControlCore() As WebControl
Dim control As New Label()
control.ID = "editor"
Return control
End Function
Protected Overrides Function CreateEditModeControlCore() As WebControl
Dim control As New DropDownList()
control.ID = "editor"
control.Items.Add("0")
control.Items.Add("1")
control.Items.Add("2")
control.Items.Add("3")
control.Items.Add("4")
control.Items.Add("5")
AddHandler control.SelectedIndexChanged, AddressOf control_SelectedIndexChanged
Return control
End Function
Private Sub control_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
EditValueChangedHandler(sender, e)
End Sub
Protected Overrides Function GetControlValueCore() As Object
Dim result As Integer = 0
If Integer.TryParse(CType(Editor, DropDownList).SelectedValue, result) Then
Return result
End If
Return 0
End Function
Protected Overrides Sub ReadEditModeValueCore()
CType(Editor, DropDownList).SelectedValue = CInt(Fix(PropertyValue)).ToString()
End Sub
Protected Overrides Sub ReadViewModeValueCore()
CType(InplaceViewModeEditor, Label).Text = CInt(Fix(PropertyValue)).ToString()
End Sub
End Class
另外,您可以重写SetupControl方法以实例化并设置为其提供自定义代码的控件。控件初始化时会调用此方法,因此建议在此处放置控件的自定义代码。
要使用CustomIntegerEditor属性编辑器编辑特定的属性值,请自定义应用程序模型。调用ASP.NET模块项目的模型编辑器,然后导航到所需的BOModel |。班级| 会员| 成员节点。将节点的IModelCommonMemberViewItem.PropertyEditorType属性设置为CustomIntegerEditor。此后,成员节点指定的属性将由CustomIntegerEditor在所有View中显示。要仅在特定的详细信息视图中使用CustomIntegerEditor属性编辑器,请使用视图|的PropertyEditorType属性| <DetailView> | 物品| 改为使用<PropertyEditor>节点。
您可能需要实现IAppearanceFormat接口,并将条件外观模块的IAppearanceFormat.BackColor,IAppearanceFormat.FontColor和IAppearanceFormat.FontStyle设置手动应用于创建的控件。