C# Note11:WPF Maskable TextBox for Numeric Values
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
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
Grid.Column
is an Attached Property.
https://www.codeproject.com/Articles/34228/WPF-Maskable-TextBox-for-Numeric-Values
Simple Numeric TextBox : https://www.codeproject.com/Articles/30812/Simple-Numeric-TextBox
C# Note11:WPF Maskable TextBox for Numeric Values