Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

Data Validation and Visual States for Windows Phone and Windows

One of the developers we’ve been working with recently suggested incorporating the INotifyDataError interface into some of the view models on a particular project. As a lot of the applications we typically  work on are more consumption style applications I haven’t seen the need to add complex data validation logic; where we do need to validate one or two values we typically just have a different visual state that represents when the user hasn’t correctly filled in the required fields.

I just had a quick hunt and found http://dotnet-redzone.blogspot.com.au/2012/12/windows-phone-8-easy-and-intuitive-way.html. This post is a great starting point for implementing visual states into a Windows Phone project. A number of controls support data validation. For example the TextBox has a ValidationStates visual state group, with states for valid and invalid data. The default template doesn’t specify what these states look like but it would be easy to modify the template to provide a visual indicator when data doesn’t validate.

Modifying the validation states for a control is fine for individual controls but if you need to modify the visual state for the rest of the page you’d need to capture the ErrorsChanged event and change the visual state of the page.

One of the biggest weaknesses of the data binding framework for both Windows and Windows Phone is that there isn’t a built in mechanism for changing visual states from within a view model. There are some solutions, such as data triggers or raising an even that is captured by the page and converted into a state change. These could be combined with the ErrorsChanged event in order to build a more versatile data validation framework.

Comments are closed