XAF之PropertyEditor

本文深入探讨了XAF框架下PropertyEditor的实现细节,包括其核心功能、自定义方式及示例代码,重点展示了如何为特定类型属性设计PropertyEditor,以及在实际场景中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    Detail View 使用一系列的PropertyEditor表现对象,对象的每一个属性都绑定到一个PropertyEditor。XAF自带了很多PropertyEditor。所有PropertyEditor的基类是PropertyEditor类。它提供了基本功能:

1.在UI总代表PropertyEditor的一个control,利用该control可以读写绑定的属性。

2.PropertyEditor使用MemberInfo属性获取它所代表的对象的属性的信息。

3.这里列举了一些通过PropertyEditor的control显示的属性: IsPasswordEditMaskDisplayFormatMaxLengthAllowEditIsCaptionVisibleImmediatePostData 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);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值