Click here to Skip to main content
12,948,567 members (62,425 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 27 Oct 2009

Validation Errors in Windows Presentation Foundation

, 27 Oct 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Collect validation errors into one place.


WPF validation is very powerful and handy, but using the default OTB features is just not enough and hardly considered convinient. I have written an article about simplifying the process of creating validation rules for commonly used rules such as a TextBox that should only take an Integer and must be between some range. In this article, I will follow up on the SimpleValidator with some features that will help aggregate the errors produced by the validation process, allowing you to use this collection to display in a single control (e.g. ListBox, TextBlock, etc.).


I would recommend you read this article just to get yourself acquainted to the SimpleValidator before proceeding. 

By using dependency properties, we can give the SimpleValidator a collection of ValidationError to manage, adding and updating as the value of the control changes. We then also bind this collection of ValidationError to our desinated error displaying control. This approach was designed for and works well with the Model-View-ViewModel pattern.

Let us go straight into some examples and usages.

Using the code  

Some new features in SimpleValidator

Dependency PropertyDescription
SimpleValidator.Name Allows you to set the name of the control that is being validated.
SimpleValidator.ErrorOutputSet a reference to the collection that the SimpleValidator will manage and update errors. 

Usage example for SimpleValidator.Name




    validator:SimpleValidator.ValidationType="{x:Type system:Int32}" />

Would produce: Age must be greater than 18. 

Usage example for SimpleValidator.ErrorOutput

<TextBox Margin="3"

    Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"



    validator:SimpleValidator.ErrorOutput="{Binding Errors}"

    validator:SimpleValidator.ValidationType="{x:Type system:Int32}" />
<TextBox Margin="3"

    Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"

    validator:SimpleValidator.Name="Working hours"



    validator:SimpleValidator.ErrorOutput="{Binding Errors}"

    validator:SimpleValidator.ValidationType="{x:Type system:Int32}" />

<!-- Display errors -->
<ItemsControl ItemsSource="{Binding Errors}"

    DisplayMemberPath="ValidationResult.ErrorContent" />

Code-behind / View Model code: 

public ObservableCollection<ValidationError> Errors { get; set; }
// ...
public Window1()
    Errors = new ObservableCollection<ValidationError>();
// ...  

Would produce:

Age must be greater than 18.
Working hours must be less than 24.

Points of Interest 

As you can imagine, having the control over the collection of validation results can be infinitely convenient. You could format the error displaying control to your taste as opposed to creating a validation error template for each control that you intend to use validation on.


Improvement ideas and general inquiries are very welcomed.


  • 26th October, 2009: Initial post  



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tri Q Tran
Software Developer (Senior) Stowe Australia
Australia Australia
Java/C++ Background.
Currently working in C#
WPF, WCF, Entity Framework

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
BlackBullet710-Oct-11 5:45
memberBlackBullet710-Oct-11 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170524.1 | Last Updated 27 Oct 2009
Article Copyright 2009 by Tri Q Tran
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid