65.9K
CodeProject is changing. Read more.
Home

Validation Errors in Windows Presentation Foundation

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Oct 27, 2009

CPOL

2 min read

viewsIcon

23214

downloadIcon

418

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

<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

<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