The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
This article describes how to enhance the WPF
TextBox and make it accept just numeric (integer and floating point) values. The second goal is make the
TextBox smart enough to make it easier to input numerics. This is an easy means to provide the
TextBox with some kind of intelligence, not just rejecting non-numeric symbols. The provided extension also allows setting minimum and/or maximum values.
If you search in the net, you will probably find some solutions for this problem where developers create their own versions of the
TextBox either by inheriting from it or creating a Custom/User Controls that include the standard WPF
TextBox. Most other solutions have one major drawback - you would need to replace your
TextBox definitions with your new
MaskTextBox. Sometimes, it is not painful, sometimes, it is. The reason I chose another solution is that in my case, such kind of changes would be painful.
The approach I’m proposing here is the usage of WPF Attached Properties, which basically are similar to Dependency Properties. The major difference among these to is that Dependency Properties are defined inside the control, but Attached Properties are defined outside. For instance,
TextBox.Text is a Dependency Property, but
Grid.Column is an Attached Property.
Background (Extending the Functionality of the TextBox)
First of all, I will define an enumeration which tells us whether the
TextBox accepts integer, decimal, or any kind of values:
public enum MaskType
The next thing would be the definition of the Attached Property.
public class TextBoxMaskBehavior
public static MaskType GetMask(DependencyObject obj)
public static void SetMask(DependencyObject obj, MaskType value)
public static readonly DependencyProperty MaskProperty =
private static void MaskChangedCallback(DependencyObject d,
Now, we specify the mask for the WPF
TextBox like this:
<TextBox local:TextBoxMaskBehavior.Mask="Integer" />
<TextBox local:TextBoxMaskBehavior.Mask="Decimal" />
and our assumption would be that the particular
TextBox would accept integer and decimal values, respectively. Whenever
TestBoxMaskBehavior.Mask is set,
MaskChangedCallback is being called. We will add the corresponding handling there.
Background (Subscribing to the TextBox Changes)
TextBox comes with an event:
PreviewTextInput, which allows to listen for text input. It also allows to cancel a particular text input by setting the
Handled property of the
true. Shown below is an illustrative example:
<TextBox PreviewTextInput="TextBox_PreviewTextInput" />
The C# code:
private static void TextBox_PreviewTextInput(object sender,
e.Handled = true;
Please note that this code is just an example of the
PreviewTextInput event usage. My solution has much more useful features rather than checking whether the input text is a number or not .
private void TextBoxPastingEventHandler(object sender, DataObjectPastingEventArgs e)
string clipboard = e.DataObject.GetData(typeof(string)) as string;
e.Handled = true;
I think that was self-explanatory .
We have covered the techniques that I used in the solution. The full source code and the demo binary are available from the links above. Here is a list of the features available for my Maskable TextBox:
- Rejects symbols other than digits, negative signs, and the decimal separator.
- Clamps to minimum/maximum values, if any specified.
- When typing negative sign, regardless from the caret position, adds it in the beginning if not exist, or removes the existing one.
- When typing the decimal separator, removes the existing one (if exists), and places the new one in the correct place.
- And, more useful stuff. Just try to use it. I’m sure you will not regret.
The origin of this article can be found at WPF Maskable TextBox for Numeric Values.