Example T236972
Visible to All Users

XAF - How to dynamically change mask settings based on the current object

This example demonstrates how to change mask settings of a certain editor dynamically (for example, based on properties of the current object).

Implementation Details

There are two ways to implement this functionality:

  1. Implement a ViewController that handles events of the current view and changes settings of required editors, as shown in the following topic: Access the Settings of a Property Editor in a Detail View.
  2. Implement a custom Property Editor (for example, a descendant of the corresponding built-in property editor) and change settings of the associated underlying control. For more information, refer to the following help section: Property Editors.

This example demonstrates the first approach. To implement it, the following classes are added:

  • DemoObject - a persistent class with TestString and Mask properties. Mask settings of the TestString property are changed based on the value of the Mask property.
  • ChangeMaskControllerBase - a platform-independent controller that handles events required to update the mask settings at the appropriate time.
  • WinChangeMaskController - a WinForms-specific controller that customizes settings of a WinForms control.
  • BlazorChangeMaskController - a Blazor-specific controller that customizes settings of a Blazor control.

Note that these approaches can be used only for detail views. To implement the same functionality in a ListView, create a ViewController for it and customize its List Editor according to specifics of the List Editor's control (see How to: Access the Grid Component in a List View). For example, in WinForms you can use the GridView.CustomColumnDisplayText event to pass the required text directly to a grid cell. You can also use the GridView.CustomRowCellEdit event to supply editors with different settings for different rows.

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

EFCore/DynamicMaskEF/DynamicMaskEF.Blazor.Server/Controllers/BlazorChangeMaskController.cs
C#
using DevExpress.ExpressApp.Blazor.Components.Models; using DevExpress.ExpressApp.Blazor.Editors; using DevExpress.ExpressApp.Blazor.Editors.Adapters; using DynamicMask.Module.BusinessObjects; using DynamicMask.Module.Controllers; namespace ChangeEditMask.Module.Web.Controllers { public class BlazorChangeMaskController : ChangeMaskControllerBase { protected override void SetControlMaskSettings(DevExpress.ExpressApp.Editors.PropertyEditor propertyEditor, EditMask mask) { if (propertyEditor is StringPropertyEditor stringEditor && stringEditor.Control is DxMaskedInputAdapter adapter) { var componentModel = adapter.ComponentModel; switch (mask) { case EditMask.Date: componentModel.MaskMode = DevExpress.Blazor.MaskMode.DateTime; componentModel.Mask = "MM/dd/yyyy"; break; case EditMask.Time: componentModel.MaskMode = DevExpress.Blazor.MaskMode.DateTime; componentModel.Mask = "hh:mm tt"; break; case EditMask.Numeric: componentModel.MaskMode = DevExpress.Blazor.MaskMode.Numeric; componentModel.Mask = "(000) 000-0000"; break; case EditMask.String: componentModel.MaskMode = DevExpress.Blazor.MaskMode.Auto; componentModel.Mask = ""; break; } } } } }
EFCore/DynamicMaskEF/DynamicMaskEF.Win/Controllers/WinChangeMaskController.cs
C#
using DevExpress.ExpressApp.Editors; using DevExpress.ExpressApp.Win.Editors; using DevExpress.XtraEditors; using DevExpress.XtraEditors.Mask; using DynamicMask.Module.BusinessObjects; using DynamicMask.Module.Controllers; namespace DynamicMask.Module.Win.Controllers { public class WinChangeMaskController : ChangeMaskControllerBase { protected override void SetControlMaskSettings(PropertyEditor propertyEditor, EditMask mask) { if (propertyEditor is StringPropertyEditor) { TextEdit textEdit = ((StringPropertyEditor)propertyEditor).Control; switch (mask) { case EditMask.Date: textEdit.Properties.Mask.MaskType = MaskType.DateTime; textEdit.Properties.Mask.EditMask = "d"; break; case EditMask.Time: textEdit.Properties.Mask.MaskType = MaskType.DateTime; textEdit.Properties.Mask.EditMask = "t"; break; case EditMask.Numeric: textEdit.Properties.Mask.MaskType = MaskType.Numeric; textEdit.Properties.Mask.EditMask = "d"; break; case EditMask.String: textEdit.Properties.Mask.MaskType = MaskType.None; textEdit.Properties.Mask.EditMask = ""; break; } } } } }
EFCore/DynamicMaskEF/DynamicMaskEF.Module/BusinessObjects/DemoObject.cs
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.Persistent.BaseImpl; using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl.EF; using DevExpress.ExpressApp.Model; namespace DynamicMask.Module.BusinessObjects { [DefaultClassOptions] public class DemoObject : BaseObject { [ImmediatePostData] public virtual EditMask Mask { get; set; } [ModelDefault("EditMask", "(000) 000-0000")] public virtual string TestString { get; set; } } public enum EditMask { Date, Time, Numeric, String } }
EFCore/DynamicMaskEF/DynamicMaskEF.Module/Controllers/ChangeMaskControllerBase.cs
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.ExpressApp; using DynamicMask.Module.BusinessObjects; using DevExpress.ExpressApp.Editors; namespace DynamicMask.Module.Controllers { public class ChangeMaskControllerBase : ObjectViewController<DetailView, DemoObject> { private PropertyEditor propertyEditor; protected override void OnActivated() { base.OnActivated(); View.CustomizeViewItemControl<PropertyEditor>(this, PropertyEditorCreated, nameof(DemoObject.TestString)); View.CurrentObjectChanged += View_CurrentObjectChanged; ObjectSpace.ObjectChanged += ObjectSpace_ObjectChanged; } void PropertyEditorCreated(PropertyEditor propertyEditor) { this.propertyEditor = propertyEditor; UpdateMaskSettings(propertyEditor); } void ObjectSpace_ObjectChanged(object sender, ObjectChangedEventArgs e) { if (propertyEditor is not null && e.PropertyName == nameof(DemoObject.Mask)) { UpdateMaskSettings(propertyEditor); } } void View_CurrentObjectChanged(object sender, EventArgs e) { if (propertyEditor is not null) { UpdateMaskSettings(propertyEditor); } } void UpdateMaskSettings(PropertyEditor propertyEditor) { if (ViewCurrentObject is not null) { SetControlMaskSettings(propertyEditor, ViewCurrentObject.Mask); } } protected virtual void SetControlMaskSettings(PropertyEditor propertyEditor, EditMask mask) { } protected override void OnDeactivated() { base.OnDeactivated(); View.CurrentObjectChanged -= View_CurrentObjectChanged; ObjectSpace.ObjectChanged -= ObjectSpace_ObjectChanged; propertyEditor = null; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.