New to Telerik UI for WPF? Download free 30-day trial

MaskedTextInput

RadMaskedTextInput represents a control that can be used to restrict the input of text values.

To use the RadMaskedTextInput control in your projects, add references to the following assemblies:

  • Telerik.Windows.Controls
  • Telerik.Windows.Controls.Input
  • Telerik.Windows.Data

Declaratively defined MaskedTextInput

The following example shows how to define the control and set few of its properties.

Define RadMaskedTextInput in XAML

<telerik:RadMaskedTextInput EmptyContent="Enter digits" 
                            InputBehavior="Replace" 
                            Mask="a20" 
                            SelectionOnFocus="SelectAll" 
                            TextMode="PlainText" 
                            UpdateValueEvent="LostFocus" 
                            Value="Sample Text" /> 

RadMaskedTextInput example

WPF RadMaskedInput RadMaskedTextInput example

Data Binding

RadMaskedTextInput's Value property is of type string and you have to bind it to ViewModel's property of type string.

Binding to object is not support and may result in unpredictable behavior.

Define the view model

public class ViewModel : ViewModelBase 
{ 
    private string text; 
 
    public ViewModel() 
    { 
        this.Text = "Sample Text"; 
    } 
 
    public string Text 
    { 
        get { return this.text; } 
        set 
        { 
            if (this.text != value) 
            { 
                this.text = value; 
                this.OnPropertyChanged("Text"); 
            } 
        } 
    } 
} 

Binding the Value property

<telerik:RadMaskedTextInput EmptyContent="Enter digits" 
                            InputBehavior="Replace" 
                            Mask="a20" 
                            SelectionOnFocus="SelectAll" 
                            TextMode="PlainText" 
                            UpdateValueEvent="LostFocus" 
                            Value="{Binding Text}" /> 

Setting the Value Mode

The value mode allows you to set the behavior of the Value property in a mask scenario (when the Mask property is set). By default the Value property holds the characters without including the placeholders and the literals defined in the mask. You can alter this and allow the value to hold also literal and placeholders by setting the ValueMode property of the control.

Read more about this in the Value Mode article.

FormatString

You can format the entered value using the FormatString property of the RadMaskedTextInput control. The property works with the standard .NET string formats. The format will be applied only when the control is unfocused.

Setting the FormatString property

<telerik:RadMaskedTextInput Value="Adam" 
                            Mask="a4"  
                            FormatString="{}UserName: {0} NickName: {0}"/> 

Showing the text value when the control is focused

WPF RadMaskedInput Showing the text value when the control is focused

Showing the text value when the control is unfocused

WPF RadMaskedInput Showing the text value when the control is unfocused

Accepts Return

The RadMaskedTextInput control allows you to span the input text on multiple lines. This is controlled via the AcceptsReturn property and its default value is True. To disable the text to be inputted on multiple lines, set the AcceptsReturn property to False.

Disable multi-line text input

<telerik:RadMaskedTextInput AcceptsReturn="False"/> 

See Also

In this article