Reputation: 42185
Users of my application can enter values into TextBoxes. In some cases the values need to be restricted to a certain range, e.g. 0 to 1, or 1 to 50. In other cases they can only enter upto 13 or 20 characters depending.
Can I use Binding.StringFormat to enforce these ranges? If so,
If not, what's the best way to implement this?
Upvotes: 3
Views: 7779
Reputation: 17631
What you want is input validation. For starters this tutorial on MSDN will help you, more information on IDataErrorInfo (preferred way) can be obtained here.
This question was already a topic on SO: WPF Data Binding and Validation Rules Best Practices
Upvotes: 3
Reputation: 223257
You need to use Input mask, Check out this article WPF Maskable TextBox for Numeric Values
Based on this article in Events TextBox_PreviewTextInput
and TextBoxPastingEventHandler
you could check for a range.
Upvotes: 1
Reputation: 2746
The Vaildation in WPF Code Project should give you a good start.
Also the related question WPF Validators like ASP.NET should assist.
Upvotes: 1