WPF MaskedTextBox Control

WPF MaskedTextBox Control

A MaskedTextBox allows us to apply mask to a TextBox. This article demonstrates how to use the MaskedTextBox control in a WPF application using C# and XAML.  

Adding Reference to WPF Toolkit Extended Assembly

The MaskedTextBox control is a part of the WPF Toolkit Extended and does not come with Visual Studio 2010. To use the Calculator control in your application, you must add reference to the WPFToolkit.Extended.dll assembly. You can download Extended WPF Tookit from the CodePlex or you can use the WPFToolkit.Extended.dll available with this download. All you need is the DLL. See Downloads section of this article. You can find more details in my blog Adding Reference to WPF Toolkit Extended.

 

Creating a MaskedTextBox

The MaskedTextBox element represents a WPF MaskedTextBox control in XAML. The MaskedTextBox control is defined in the System.Windows.Controls namespace. Listing 1 creates a simple MaskedTextBox control. 

<wpfx:MaskedTextBox Width="200" Height="30" Mask="(000) 000-0000" /> 

Listing 1 

The MaskedTextBox looks like Figure 1.

WpfMTB1.jpg

Figure 1

 

Mask and Value

The Mask property represents a mask applied to the control and the Value property is the string to be displayed in text area. The code snippet in Listing 2 sets both of these values. 

<wpfx:MaskedTextBox Width="200" Height="30" Mask="(000) 000-0000" Value="4849898984"/> 

Listing 2 

The output of Listing 2 generates output that looks like Figure 2. 

 WpfMTB2.jpg

Figure 2

Supported Masks

The following table describes the mask elements.

 

Mask Element

Description

0

Digit, required.This element will accept any single digit between 0 and 9.

9

Digit or space,optional.

#

Digit or space,optional. If this position is blank in the mask, it will be rendered as aspace in the Text property. Plus (+) and minus (-) signs are allowed.

L

Letter, required.Restricts input to the ASCII letters a-z and A-Z. This mask element is equivalentto [a-zA-Z] in regular expressions.

?

Letter, optional.Restricts input to the ASCII letters a-z and A-Z. This mask element isequivalent to [a-zA-Z]? in regular expressions.

&

Character, required.If the AsciiOnly property is set to true, this element behaves like the"L" element.

C

Character, optional.Any non-control character. If the AsciiOnly property is set to true, thiselement behaves like the "?" element.

A

Alphanumeric,optional. If the AsciiOnly property is set to true, the only characters itwill accept are the ASCII letters a-z and A-Z.

a

Alphanumeric,optional. If the AsciiOnly property is set to true, the only characters itwill accept are the ASCII letters a-z and A-Z.

.

Decimal placeholder.The actual display character used will be the decimal symbol appropriate tothe format provider, as determined by the control's FormatProvider property.

,

Thousandsplaceholder. The actual display character used will be the thousandsplaceholder appropriate to the format provider, as determined by thecontrol's FormatProvider property.

:

Time separator. Theactual display character used will be the time symbol appropriate to theformat provider, as determined by the control's FormatProvider property.

/

Date separator. Theactual display character used will be the date symbol appropriate to theformat provider, as determined by the control's FormatProvider property.

$

Currency symbol. Theactual character displayed will be the currency symbol appropriate to theformat provider, as determined by the control's FormatProvider property.

<

Shift down. Convertsall characters that follow to lowercase

>

Shift up. Convertsall characters that follow to uppercase.

Disable a previousshift up or shift down.

\

Escape. Escapes amask character, turning it into a literal. "\\" is the escapesequence for a backslash.

 

Summary

In this article, we discussed how to use the MaskedTextBox control in a WPF application using C# and XAML.  

 


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.