Chapter 18. Input validation
This chapter covers
- Validating properties in code using exceptions
- Using IDataErrorInfo and INotifyDataErrorInfo for validation
- Controlling DataGrid and DataForm validation through attributes
The previous two chapters covered the binding system and some of its important uses within Silverlight. The binding system is also so important because of the input validation approaches in Silverlight built directly on it.
Validation is something almost every nontrivial application with a TextBox will need to do at some point. Often, you punt and do simple checking in the code-behind. But if you want to truly leverage binding, take advantage of patterns such as the ViewModel pattern, and have better structure to your code, you’ll want to use one of the established validation mechanisms provided by Silverlight.
Silverlight provides several ways to validate data. The simplest and oldest approach is to use exception-based validation. In that approach, property setters throw exceptions when the validation doesn’t pass. This is the code equivalent of my toddler’s spitting out the food she doesn’t like and, like that, anything more complex than a couple of peas is going to get pretty messy.