Chapter 13. Input validation
This chapter covers
- Validating properties in code using exceptions
- Using IDataErrorInfo and INotifyDataErrorInfo for validation
- Controlling DataGrid and DataForm validation through attributes
In chapter 11, we covered binding. In chapter 12, I mentioned that I believe binding to be one of the single most important topics for Silverlight developers. Another reason for that is because all the validation approaches covered in this chapter build directly on the binding system.
Validation is something almost every nontrivial application with a TextBox will need to do at some point. Often, we 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 just 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 simply 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.