Thursday, January 2, 2014

Validating a WPF textbox to accept only negative decimal numbers

Recently working on one of the project, I came across an requirement of validating a TextBox in which Textbox should allow numbers only between range -999 and 0.

If you will surf on internet, you will perhaps find many solutions to this problem in which developers create their own versions of TextBox either by creating a Custom/User controls or by inheriting it. But problem of this approach is - you would need to replace your TextBox definitions with your newly created TextBox. Sometimes this solution can be opted but sometimes it is not feasible to do so.

This article describes on how to enhance existing WPF TextBox to make it happen.So, the approach I am proposing here doesn't require the replacement of existing TextBox control. Whilst it uses the very basic concept of Events. Well, enough of theory. Let's move on to coding part:

XAML code:






Code-Behind code:

Hope you like this clean approach.

No comments:

Post a Comment