EPR类企业管理系统

在我们现有系统基础上或全新开发,提供定制服务
为您的企业高效快速实施ERP,WMS,MES,CRM管理系统
全面管控物料仓库、销售业务、采购业务、仓库业务
生产过程、质量检验、组织架构、业务报表


定制
QQ:460-3528

开发
QQ群:3360-90194

源码
微信:136-3650-3721

如何:自定义条件外观(Conditional Appearance)模块的行为

有条件的外观模块可让您根据指定的条件不同的外观适用于UI元素。因此,您可以根据当前查看的对象状态进行可见/不可见或已启用/禁用的操作。此外,该模块使您可以将条件外观应用于“详细信息视图”中的“属性编辑器”,“列表视图”中的属性以及布局项目,组和选项卡。您可以为编辑器和布局项目标题设置字体颜色,样式和背景色。要为目标UI元素定义所需的条件外观,请在代码或应用程序模型中声明条件外观规则。但是,在某些情况下,规则提供的条件出现是不够的。在这种情况下,请使用AppearanceController公开的事件。在本主题中,您将找到有关如何处理这些事件以完成任务的几个示例。

条件外观模块提供AppearanceController。该控制器公开以下事件。

  • 外观适用

    将条件外观应用于指定的UI元素后,可以进行更改。

  • CustomApplyAppearance

    允许您覆盖应用自定义的默认过程,或取消为目标UI元素应用条件外观的过程。

  • 收集外观规则

    允许您动态添加条件外观规则。

注意

移动平台不支持本主题中描述的方法。

提示

DevExpress代码示例数据库(http://www.devexpress.com/example=E3595)中提供了完整的示例项目。

处理AppearanceApplied事件

在下面的示例中,我们将重置应用于Product类的Category属性的条件外观。根据应用于产品类的条件外观规则,当在列表视图中将其设置为“海鲜”时,将使用蓝色显示其类别属性。However, when an object, to which this appearance is applied, is selected, the property's value is close to invisible. 参见下图。

ConditionalAppearance_HowTo1

using DevExpress.ExpressApp.ConditionalAppearance;
using DevExpress.ExpressApp.Editors;
//...
[DefaultClassOptions]
[Appearance("CategoryColoredInListView", AppearanceItemType = "ViewItem", TargetItems = "Category",
    Criteria = "Category = 'Seafood'", Context = "ListView", FontColor = "Blue", Priority = 1)]
public class Product : BaseObject {
    public Product(Session session) : base(session) {}    
    private Category category;
    public Category Category {
        get {
            return category;
        }
        set {
            SetPropertyValue(nameof(Category), ref category, value);
        }
    }
}

因此,如果当前选择了上下文对象,则应在列表视图中重置Category属性的条件外观。为此,我们使用AppearanceController.AppearanceApplied事件。在下面的代码中,要重置当前已应用条件外观的项目的字体颜色,请将此项目强制转换为IAppearanceFormat接口。在您的任务中,您还可以使用IAppearanceEnabledIAppearanceVisibility接口访问条件外观属性。

using DevExpress.ExpressApp.ConditionalAppearance;
using ConditionalAppearanceExample.Module.BusinessObjects;
using DevExpress.ExpressApp.Editors;
//...
public partial class ConditionalAppearanceController : ViewController {
    public ConditionalAppearanceController() {
        InitializeComponent();
        TargetObjectType = typeof(Product);
    }
    private AppearanceController appearanceController;
    protected override void OnActivated() {
        base.OnActivated();
        appearanceController = Frame.GetController<AppearanceController>();
        if (appearanceController != null) {
            appearanceController.AppearanceApplied  = 
                new EventHandler<ApplyAppearanceEventArgs>(
                    appearanceController_AppearanceApplied);
        }
    }
    void appearanceController_AppearanceApplied(
        object sender, ApplyAppearanceEventArgs e) {         
        if ((View is ListView) && (e.ItemType == AppearanceItemType.ViewItem.ToString()) &&
                 (e.ItemName == "Category") && (e.ContextObjects.Length > 0)) {
            if (View.SelectedObjects.Contains(e.ContextObjects[0])) {
                IAppearanceFormat formattedItem = e.Item as IAppearanceFormat;
                if (formattedItem != null) {
                    formattedItem.ResetFontColor();
                }
            }
        }
    }
    protected override void OnDeactivated() {
        if (appearanceController != null) {
            appearanceController.AppearanceApplied -=
                 new EventHandler<ApplyAppearanceEventArgs>(
                     appearanceController_AppearanceApplied);
        }
        base.OnDeactivated();
    }
}

使用此代码,Category属性变得更容易阅读。

ConditionalAppearance_HowTo2

处理CustomApplyAppearance事件

在下一个示例中,我们将自定义由条件外观规则禁用的属性编辑器的外观。为了演示,我们已将DisabledProperty添加到Product业务类,并对其应用了条件外观规则。此规则使该属性始终处于禁用状态。

using DevExpress.ExpressApp.ConditionalAppearance;
using DevExpress.ExpressApp.Editors;
//...
[DefaultClassOptions]
[Appearance("CategoryColoredInListView", AppearanceItemType = "ViewItem", _
TargetItems = "Category", Criteria = "Category = 'Seafood'", Context = "ListView", _
FontColor = "Blue", Priority = 1)]
public class Product : BaseObject {
    public Product(Session session) : base(session) {}    
    private string disabledProperty;
    [Appearance("DisableProperty", Criteria = "1=1", Enabled = false)]
    public string DisabledProperty {
        get {
            return disabledProperty;
        }
        set {
            SetPropertyValue(nameof(DisabledProperty), ref disabledProperty, value);
        }
    }
}

要更改禁用的属性编辑器的外观,我们使用AppearanceController.CustomApplyAppearance事件。在下面的代码中,更改了AppearanceController刚刚应用了条件外观的项目的边框样式。此项不能转换为IAppearanceFormat接口,因为它不允许您更改边框样式。因此,我们将其转换为DXPropertyEditorWebPropertyEditor类(因为我们知道它分别是Windows Form和ASP.NET应用程序中所有内置属性编辑器的基类)。请参阅AppearanceController.CustomApplyAppearance事件描述,以了解可以将哪些项目类型作为项目传递AppearanceAppliedCustomApplyAppearance事件处理程序中的参数。

要取消其他可能的条件外观更改并仅应用禁用该项目的规则,我们将事件处理程序的Handled参数设置为true

由于这里需要访问平台相关的类,因此必须在应用程序解决方案的相应模块中实现Windows Forms和ASP.NET Controllers。

//The base platform-independent Controller
using DevExpress.ExpressApp.ConditionalAppearance;
using ConditionalAppearanceExample.Module.BusinessObjects;
using DevExpress.ExpressApp.Editors;
//...
public partial class ConditionalAppearanceController : ViewController {
    public ConditionalAppearanceController() {
        InitializeComponent();
        TargetObjectType = typeof(Product);
    }
    private AppearanceController appearanceController;
    protected override void OnActivated() {
        base.OnActivated();
        appearanceController = Frame.GetController<AppearanceController>();
        if (appearanceController != null) {
            appearanceController.CustomApplyAppearance  = 
                new EventHandler<ApplyAppearanceEventArgs>(
                    appearanceController_CustomApplyAppearance);
        }
    }
    void appearanceController_CustomApplyAppearance(
        object sender, ApplyAppearanceEventArgs e) {         
        if (e.AppearanceObject.Enabled == false) {
            CustomizeDisabledEditorsAppearance(e);
            // e.Handled = true;
        }
    }
    protected virtual void CustomizeDisabledEditorsAppearance(ApplyAppearanceEventArgs e) { }
    protected override void OnDeactivated() {
        if (appearanceController != null) {
            appearanceController.CustomApplyAppearance -=
                 new EventHandler<ApplyAppearanceEventArgs>(
                     appearanceController_CustomApplyAppearance);
        }
        base.OnDeactivated();
    }
}
注意

您可以将e.Handled参数设置为true以取消其他可能的外观更改。但是,在这种情况下,您将需要手动禁用编辑当前控制值。

//Controller implemented in a Windows Forms module
using DevExpress.ExpressApp.ConditionalAppearance;
using ConditionalAppearanceExample.Module.Controllers;
using DevExpress.ExpressApp.Win.Editors;
using DevExpress.XtraEditors.Controls;
using System.Drawing;
//...
public partial class WinConditionalAppearanceController : ConditionalAppearanceController {
    protected override void CustomizeDisabledEditorsAppearance(
        ApplyAppearanceEventArgs e) {
        base.CustomizeDisabledEditorsAppearance(e);
        DXPropertyEditor dxEditor = e.Item as DXPropertyEditor;
        if (dxEditor != null && dxEditor.Control != null) {
            dxEditor.Control.Properties.BorderStyle = BorderStyles.Simple;
            dxEditor.Control.Properties.Appearance.BackColor = Color.RosyBrown;
        }
    }
}
//Controller implemented in an ASP.NET module
using ConditionalAppearanceExample.Module.Controllers;
using DevExpress.ExpressApp.ConditionalAppearance;
using DevExpress.ExpressApp.Web.Editors;
using DevExpress.ExpressApp.Web.Editors.ASPx;
using System.Web.UI.WebControls;
using System.Drawing;
//...
public partial class WebConditionalAppearanceController : ConditionalAppearanceController {
    protected override void CustomizeDisabledEditorsAppearance(
        ApplyAppearanceEventArgs e) {
        base.CustomizeDisabledEditorsAppearance(e);
        WebPropertyEditor dxEditor = e.Item as WebPropertyEditor;
        if (dxEditor != null && dxEditor.Editor != null) {
            dxEditor.Editor.BorderStyle = BorderStyle.Dashed;
            dxEditor.Editor.BackColor = Color.RosyBrown;
        }
    }
}

下图演示了结果:

ConditionalAppearance_HowTo3

注意

您可以自定义外观对象,而不是直接访问编辑器的控件。遍历ApplyAppearanceEventArgs.AppearanceItems列表以选择所需的项目。

处理CollectAppearanceRules事件

使用AppearanceController.CollectAppearanceRules事件,可以动态添加条件外观规则。例如,状态机模块处理此事件以添加在运行时声明的外观规则作为状态对象的集合。这些外观规则是实现IAppearanceRuleProperties接口的持久对象。

public class StateMachineAppearanceController : StateMachineControllerBase<ObjectView> {
//...
    private void appearanceController_CollectAppearanceRules(
        object sender, CollectAppearanceRulesEventArgs e) {
        IList<IStateMachine> stateMachines = GetStateMachines();
        foreach(IStateMachine stateMachine in stateMachines) {
            foreach(IState state in stateMachine.States) {
                if(state is IStateAppearancesProvider) {
                    e.AppearanceRules.AddRange(((IStateAppearancesProvider)state).Appearances);
                }
            }
        }
    }
}

ConditionalAppearance_HowTo4

注意

在为指定的UI元素收集规则后,外观控制器将其缓存,以便下次刷新该UI元素的条件外观时,不会再次收集规则。由于一旦为UI元素收集了规则,就会引发AppearanceController.CollectAppearanceRules事件,因此订阅该元素可能为时已晚,因为规则已被缓存。我们建议您调用AppearanceController.ResetRulesCache方法,然后订阅AppearanceController.CollectAppearanceRules事件,最后,调用AppearanceController.Refresh方法。

“条件外观”模块在XAF随附的Feature Center演示中进行了演示。要查看实际使用的模块,请在线参考Feature Center演示。

相关文章

转载保留此链接,注明出处