XAF-如何修改内置的编辑器(Property Editor)

本示例演示在web/win中给 日期选择控制显示出一个时钟及修改时间的控件。效果如下:

如果你装了XAF在这个路径中已经有了这个示例:

%PUBLIC%DocumentsDevExpress Demos 16.2ComponentseXpressApp FrameworkFeatureCenter.

在线也有一个版本: http://demos.devexpress.com/XAF/FeatureCenter/.

一、继承属性编辑器
 ASP.NET 模块项目,建个新类出来,继承ASPxDateTimePropertyEditor 
using System;
using System.Web.UI.WebControls;
using DevExpress.Web;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Web.Editors.ASPx;
//... 
[PropertyEditor(typeof(DateTime), false)]
public class CustomDateTimeEditor : ASPxDateTimePropertyEditor {
    public CustomDateTimeEditor(Type objectType, IModelMemberViewItem info) : 
        base(objectType, info) { }
    protected override void SetupControl(WebControl control) {
        base.SetupControl(control);
        if(ViewEditMode == ViewEditMode.Edit) {
            ASPxDateEdit dateEdit = (ASPxDateEdit)control;
            dateEdit.TimeSectionProperties.Visible = true;
            dateEdit.UseMaskBehavior = true;
        }
    }
}

如果是win项目:

using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Win.Editors;
//... 
[PropertyEditor(typeof(DateTime), false)]
public class CustomDateTimeEditor : DatePropertyEditor {
    public CustomDateTimeEditor(Type objectType, IModelMemberViewItem info) : 
        base(objectType, info) { }
    protected override void SetupRepositoryItem(RepositoryItem item) {
        base.SetupRepositoryItem(item);
        RepositoryItemDateTimeEdit dateProperties = (RepositoryItemDateTimeEdit)item;
        dateProperties.CalendarTimeEditing = DefaultBoolean.True;
        dateProperties.CalendarView = CalendarView.Vista;
    }
}

二、应用这个编辑器

上面的代码写完了,编译一下,重新打开xafml.

找到bo,找到日期型属性,在PropertyEditor中找到CustomDateTimeEditor.

在xafml中设置日期属性的displayformat和editmask

或直接在bo中设置。

using DevExpress.ExpressApp.Model;
//... 
[ModelDefault("DisplayFormat", "{0:MM.dd.yyyy hh:mm:ss}")]
[ModelDefault("EditMask", "MM.dd.yyyy hh:mm:ss")]
public DateTime CreatedOn { get; set;}

运行项目,去看结果吧!

win的效果如下:
原文地址:https://www.cnblogs.com/foreachlife/p/custompropertyeditor.html