Back to Devexpress

TextEdit.CustomMask Event

wpf-devexpress-dot-xpf-dot-editors-dot-textedit-ca867caa.md

latest9.5 KB
Original Source

TextEdit.CustomMask Event

Occurs after the editor is initialized, got the focus, or the editor’s value is changed.

Namespace : DevExpress.Xpf.Editors

Assembly : DevExpress.Xpf.Core.v25.2.dll

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
public event EventHandler<CustomMaskEventArgs> CustomMask
vb
Public Event CustomMask As EventHandler(Of CustomMaskEventArgs)

Event Data

The CustomMask event's data class is CustomMaskEventArgs. The following properties provide information specific to this event:

PropertyDescription
ActionTypeGets the user action.
CurrentCursorPositionGets the cursor (caret) position before a user performs an action.
CurrentEditTextGets text displayed in the editor before a user performs an action.
CurrentHeadGets the editor’s text that precedes the cursor (caret) position or selected text.
CurrentSelectedTextGets the editor’s selected text.
CurrentSelectionAnchorGets the selection start point before a user performs an action.
CurrentSelectionEndGets the position of the right-most selected character.
CurrentSelectionStartGets the position of the left-most selected character.
CurrentTailGets the editor’s text that follows the cursor (caret) position or selected text.
HandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
InsertedTextGets text a user attempts to add to the editor.
IsCanceledGets whether the user action does not lead to the editor value change.
IsSelectionGets whether the editor contains selected text.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
ResultCursorPositionGets the cursor (caret) position after a user performs an action.
ResultEditTextGets text displayed in the editor after a user performs an action.
ResultSelectionAnchorGets the selection start point after a user performs an action.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
SourceGets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.
TagGets the editor’s Tag property value.

The event data class exposes the following methods:

MethodDescription
Cancel()Cancels the user action.
InvokeEventHandler(Delegate, Object)When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object)When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.
SetResult(String, Int32, Nullable<Int32>)Sets the editor text, cursor position, and selection anchor.
SetResult(String, String)Sets the editor text and cursor position.
ToString()Gets the transcript of the user’s action in the following format: (InitialState)->UserAction->(FinalState).

Remarks

You can use custom masks to process user input and specify complex custom conditions. Do the following to specify a custom mask:

  1. Set the editor’s MaskType property to Custom.
  2. Handle the CustomMask event.
  3. Process the text displayed in the editor (e.CurrentEditText), the text entered by a user (e.InsertedText), or the edited text (e.ResultEditText).
  4. Use e.SetResult or e.Cancel methods to specify the editor’s final text.

The following code sample allows users to enter only valid image file names:

xaml
<dxe:TextEdit MaskType="Custom" CustomMask="OnCustomMask"/>
csharp
void OnCustomMask(object sender, DevExpress.Xpf.Editors.CustomMaskEventArgs e) {
    var imageExtensions = new string[] {".jpeg", ".jpg", ".png", ".gif", ".tiff", ".tif", ".bmp", ".svg", ".esp"};
    var enteredExtension = Path.GetExtension(e.ResultEditText);

    if (e.ResultEditText.Any(x => Path.GetInvalidFileNameChars().Contains(x)))
        e.Cancel();

    if (!imageExtensions.Any(x => x.StartsWith(enteredExtension, System.StringComparison.OrdinalIgnoreCase)))
        e.Cancel();
}
vb
Private Sub OnCustomMask(ByVal sender As Object, ByVal e As DevExpress.Xpf.Editors.CustomMaskEventArgs)
    Dim imageExtensions = New String() {".jpeg", ".jpg", ".png", ".gif", ".tiff", ".tif", ".bmp", ".svg", ".esp"}
    Dim enteredExtension = Path.GetExtension(e.ResultEditText)

    If e.ResultEditText.Any(Function(x) Path.GetInvalidFileNameChars().Contains(x)) Then e.Cancel()

    If Not imageExtensions.Any(Function(x) x.StartsWith(enteredExtension, System.StringComparison.OrdinalIgnoreCase)) Then e.Cancel()
End Sub

Refer to the following help topic for more information: Mask Type: Custom.

See Also

TextEdit Class

TextEdit Members

DevExpress.Xpf.Editors Namespace