Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / WPF

Validation Errors in Windows Presentation Foundation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
27 Oct 2009CPOL2 min read 23K   418   12   1
Collect validation errors into one place.

Introduction  

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.).

Background 

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

XML
<TextBox
    validator:SimpleValidator.Name="Age"
    validator:SimpleValidator.LowerRange="18"
    validator:SimpleValidator.ValidationType="{x:Type system:Int32}" />

Would produce: Age must be greater than 18. 

Usage example for SimpleValidator.ErrorOutput

XML
<TextBox Margin="3"
    Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"
    validator:SimpleValidator.Name="Age"
    validator:SimpleValidator.LowerRange="18"
    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.LowerRange="0"
    validator:SimpleValidator.UpperRange="24"
    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>();
    InitializeComponent();
}
// ...  

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.

Feedback

Improvement ideas and general inquiries are very welcomed.

History 

  • 26th October, 2009: Initial post  

 

License

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


Written By
Software Developer (Senior) Stowe Australia
Australia Australia
Java/C++ Background.
Currently working in C#
WPF, WCF, ADO.net Entity Framework

Comments and Discussions

 
GeneralMy vote of 5 Pin
BlackBullet710-Oct-11 5:45
BlackBullet710-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.