WPF Validation Rules

Wow – just reading the section on Advanced DataBinding in the WPF Unleashed book. This talks about how to do validation during data binding – specifically by creating custom classes that inherit from ValidationRule and return a ValidationResult. Apart from being very déjà-vu – because at work we have our own validation rules engine that uses exactly the same class names it also sounds a little odd. Having a ValidationRules collection hanging right off the Binding itself – is that really the best place to define the rules?

My experience with our work validation engine is that certainly many of the rules – the simple ones – are actually derived directly from the binding. Therefore it would make sense to have them directly associated. Rules such as “is the field mandatory”, “what is the fixed range”, “what is the maximum text length”, “what is the precision associated with a number”. But what about inter-field rules such as “field A must be greater than field B”, “field C is mandatory when field D is set to value x”. Those are really entity based rules – not field based. Do we/could we put them at the DataBinding level?

I guess I’ll have to keep reading to find out :-p


Ok – so now I’ve given this a quick test. To start with I’ve just created a simple class to bind to (Person) and hooked up a TextBox.

    <TextBox SpellCheck.IsEnabled="True">
            <Binding Source="{StaticResource nos}" Path="FirstName" 
UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <valid:MandatoryRule/> </Binding.ValidationRules> <Binding.NotifyOnValidationError>true</Binding.NotifyOnValidationError> </Binding> </TextBox.Text> </TextBox>

Ok – the spell checking was just for fun.

Pretty longwinded isn’t it. Setting the NotifyOnValidationError means that I can hook my Status Panel up to the events so that entries are added and removed. The ValidationErrorEventArgs has a host of properties that allows me to determine exactly which Binding and UI element. Also, because the error content is an object I could provide my own Error class – so instead of just returning a ValidationResult with a string (as per example below) I could generate a rich error instance (with primary/secondary text, priority, help URL etc.)

My MandatoryRule is as simple as can be.

    public class MandatoryRule : ValidationRule
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
if (value == null || object.Equals(value, string.Empty))
return new ValidationResult(false, "The field is mandatory.");
                return ValidationResult.ValidResult;

Hopefully I will put all this together and post soon with a working example of the Status Panel, together with some ideas on other features that could be included to make it more functional – such as clicking on a message to focus the associated UI element.

6 thoughts on “WPF Validation Rules”

  1. Error 1 ”valid’ is an undeclared namespace. Line 20, position 18.’ XML is not valid.

  2. Patrick, “valid” is just the namespace where the MadatoryRule class has been defined. This should be included at the root level of your control/window eg.


  3. Oops, in the last line of my previous post, the example namespace should appear after the “clr-namespace:”

    ie. clr-namespace:%lt;MyNamespace>

Comments are closed.