How to: Disable and Hide Property Editors Based on a Business Rule

XAF is shipped with the Conditional Appearance module. One of the features provided by this module is an option to disable/enable and show/hide Property Editors based on business rules. This topic contains step-by-step instructions that demonstrate how the Conditional Appearance module can be used for this purpose. Several appearance rules will be created to dynamically hide and disable Property Editors.

EditorStateExample

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E1672.

Note

See more examples on how to use the Conditional Appearance in the Feature Center demo installed with XAF, or refer to the Feature Center demo online.

 

Create a new XAF Solution using the DevExpress v18.2 XAF Solution Wizard template. Add the Conditional Appearance module to the module and Mobile module project. To do this, invoke the Module Designer for the module (Mobile module) project and drag the ConditionalAppearanceModule (ConditionalAppearanceMobileModule) item from the Toolbox to the Required Modules panel.

Tutorial_EM_Lesson_5_1

Declare the Contact business class in the solution's Module Project. You can use the XPO Business Object template for this purpose. Replace the auto-generated class' code with the following.

[DefaultClassOptions]
[ImageName("BO_Person")]
public class Contact : BaseObject {
    public Contact(Session session) : base(session) { }        
    public string Name {
        get { return GetPropertyValue<string>("Name"); }
        set { SetPropertyValue<string>("Name", value); }
    }
    public bool IsMarried {
        get { return GetPropertyValue<bool>("IsMarried"); }
        set { SetPropertyValue<bool>("IsMarried", value); }
    }
    public string SpouseName {
        get { return GetPropertyValue<string>("SpouseName"); }
        set { SetPropertyValue<string>("SpouseName", value); }
    }
    public string Address1 {
        get { return GetPropertyValue<string>("Address1"); }
        set { SetPropertyValue<string>("Address1", value); }
    }
    public string Address2 {
        get { return GetPropertyValue<string>("Address2"); }
        set { SetPropertyValue<string>("Address2", value); }
    }
}

The sample Contact class contains five properties: Name, IsMarried, SpouseName, Address1, Address2.

To ensure that the SpouseName Property Editor is only displayed when a contact is married, add the following AppearanceAttribute attribute. The IsMarried property should be decorated by the ImmediatePostDataAttribute attribute to ensure that the SpouseName Property Editor is immediately hidden when a user changes the IsMarried value.

using DevExpress.ExpressApp.ConditionalAppearance;
using DevExpress.ExpressApp.Editors;
//...
[ImmediatePostData]
public bool IsMarried {
    // ...
}
[Appearance("Single", Visibility = ViewItemVisibility.Hide, Criteria = "!IsMarried", Context="DetailView")]
public string SpouseName {
    // ...
}

The appearance rule, declared by the following attribute, ensures that the Address2 Property Editor is enabled only if the Address1 field is filled. Otherwise, the Address2 Property Editor should is disabled.

[ImmediatePostData]
public string Address1 {
    // ...
}
[Appearance("AddressOneIsEmpty", Enabled = false, Criteria = "IsNullOrEmpty(Address1)", Context="DetailView")]
public string Address2 {
    // ...
}
See Also