Detail View 使用一系列的PropertyEditor表现对象,对象的每一个属性都绑定到一个PropertyEditor。XAF自带了很多PropertyEditor。所有PropertyEditor的基类是PropertyEditor类。它提供了基本功能:
1.在UI总代表PropertyEditor的一个control,利用该control可以读写绑定的属性。
2.PropertyEditor使用MemberInfo属性获取它所代表的对象的属性的信息。
3.这里列举了一些通过PropertyEditor的control显示的属性: IsPassword, EditMask, DisplayFormat, MaxLength, AllowEdit, IsCaptionVisible, ImmediatePostData and Caption.
注意:
自定义的PropertyEditor需要在module project中实现,且要加上PropertyEditorAttribute特性。这样才能被加载到Application Model中,从而使用到UI中。
实现PropertyEditor的典型步骤如下:
1.重写CreateControlCore方法。创建并返回要求的control的实例。订阅该control的ValueChanged事件来调用WriteValue方法。
2.重写GetControlValueCore方法。返回l特定于contro的值。
3.重写ReadValueCore方法。将PropertyEditor的PropertyValue附加到control的绑定的属性上。
下面这个例子是为SingleChoiceAction类型的属性创建一个PropertyEditor,且是以Navigation形式查看:
其中 IActionContainer.Register 方法是创建代表action的control,如ActionContainerBarItem Action Container会为SimpleAction创建 BarButtonItem ,为 SingleChoiceAction创建BarEditItemcontrol, etc.
[PropertyEditor(typeof(SingleChoiceAction), FeatureCenterEditorAliases.NavigationContainerEditor)]
public class NavigationActionContainerPropertyEditor : PropertyEditor {
private NavigationActionContainer NavigationActionContainer {
get { return (NavigationActionContainer)Control; }
}
private NavigationObject NavigationObject {
get {
return (NavigationObject)CurrentObject;
}
}
protected override object CreateControlCore() {
return new NavigationActionContainer();
}
protected override void ReadValueCore() {
NavigationActionContainer.Register((SingleChoiceAction)PropertyValue, NavigationObject.NavigationStyle);
}
protected override object GetControlValueCore() {
return null;
}
public NavigationActionContainerPropertyEditor(Type objectType, IModelMemberViewItem info) : base(objectType, info) { }
}
下面给出另一个例子,为int类型的属性设计一个星级PropertyEditor:
using System;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Win.Editors;
//...
[PropertyEditor(typeof(Int32), false)]
public class CustomIntegerEditor : WinPropertyEditor {
StarRatingControl control = null;
public CustomIntegerEditor(Type objectType, IModelMemberViewItem info) :
base(objectType, info) { }
protected override object CreateControlCore() {
control = new StarRatingControl();
control.SelectedStarChanged += new EventHandler(control_SelectedStarChanged);
control.MaximumSize = control.MinSize;
ControlBindingProperty = "SelectedStar";
return control;
}
private void control_SelectedStarChanged(object sender, EventArgs e) {
OnControlValueChanged();
}
protected override void Dispose(bool disposing) {
if(control != null) {
control.SelectedStarChanged -= new EventHandler(control_SelectedStarChanged);
}
base.Dispose(disposing);
}
}