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:
- 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. - 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 withTestString
andMask
properties. Mask settings of theTestString
property are changed based on the value of theMask
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
- Multi-currency DisplayFormats in a ListView
- Formatting depending on another field value
- How to apply a (DisplayFormat) Mask to a value shown in a ListView?
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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;
}
}
}
}
}
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;
}
}
}
}
}
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
}
}
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;
}
}
}