Validation Scopes: Draft
In 2006 I published an article on Code Project explaining WPF Validation in depth. It was written for the .NET 3.0 version of WPF, and given that some of the validation capabilities have changed since then, the article has become somewhat outdated. One thing that I feel hasn’t changed is the general feeling that WPF’s validation system is limited in features, and very hard to extend. In the article I included code for an example ErrorProvider, which although it had problems, surprisingly became quite popular over time.
Over the past couple of weeks between client engagements and preparing to move to Brisbane, I’ve been working on a replacement to the ErrorProvider control that extends the possibilities of WPF validation and also fixes many of the issues of the previous attempt. As it is still a beta, I haven’t quite decided where to house the code yet, but I’m attaching it to this blog entry so that you can have a play.
I’d really appreciate if you could skim the PDF to get a sense of the design, and play with the code. In particular, check out the sample application.
What does this provide over WPF’s inbuilt validation?
The control is designed as a reusable component that exposes a heap of properties and interfaces that you can plug into around the validation process. Some of the scenarios that it enables are:
- Control over when validation happens via Validation Triggers
- As soon as the data loads
- Only after the control has had focus
- Only explicitly (hitting "Save")
- Support for rich error information. Categories, help topics, localization, and so on.
- Lists of errors
- Errors around multiple controls
- Automatically give focus to the first invalid control
- Nesting validation scopes and error list rollups
- Pluggable validation providers. Don’t like IDataErrorInfo? Want to use EntLib natively? Roll your own! (I’d love someone to contribute an EntLib provider)
The code includes a sample application to show it all in action. Have a go at playing with the various Validation Triggers.
Nesting Validation Scopes
This is one I’m keen to get feedback on. It’s explained in more depth in the document, but effectively it enables validation scopes to be nested and to aggregate the validation results at a higher level, by leveraging WPF’s hierarchical nature and the visual tree (the visual tree is not strictly required, but it’s a great case of convention over configuration). Here’s a picture from the document that might help to visualize it:
When the root Validation Scope is validated, it validates the controls it is associated with, and also the child Validation Scopes. It aggregates their results, so that if any child is invalid, the parent is invalid. This makes it great for implementing validation rollups:
A list of error messages could be placed on each tab with the messages just for that tab, or at the root of the window with all messages.
Field association
One of the departures I made from the existing validation system is that Bindings are not used as the target for showing the validation messages. This means you can use other controls to show validation messages instead. In the document, I describe how to implement something like this:
The Document
As I said, I’d really like your feedback on the attached document, as it will form the basis of my Code Project article on using the control and the documentation. Here’s the table of contents to whet your appetite:
- Background
- Introduction
- Getting Started
- Download
- Usage
- Concepts
- Validation Triggers
- Validation Providers
- Validation Display Strategies
- Validation Scope Nesting
- How To
- How To: Trigger validation when a user hits a button
- How To: Show a validation result on a control (Style)
- How To: Show a validation result on a control with an Adorner (Style + Template)
- How To: Show a validation result around multiple controls
- How To: Show extra information about an error
- How To: Show a list of validation messages
- How To: Show validation rollups on a tab control
- Known Issues
- Issue #1: Tab Controls with Validation Scopes don’t validate until selected
- Issue #2: Virtualizing Panels
- Conclusion
- Thanks
Silverlight Soon
You’ll find that the attached code does not build unless you "Unload" the Bindable.Windows.Silverlight project. I did have the same validation system working in Silverlight, but I was making heavy changes today and got fed up with trying to keep the two projects in sync. I’m thinking of instead using the one project and using partial methods and classes to manage the SL/WPF differences. As soon as I’m happy with the WPF version I’ll finish the SL port.
I hope you see something valuable. Feedback much appreciated.
Filed under: WPF, Projects | 13 Comments »
