Windows Presentation Foundation allows the developers to create attractive Windows applications by provided styles, graphics etc. One the major pain points of using WPF is that it lacks the validation framework. Microsoft WPF team did not put much effort in creating a reliable validation system. In this article we are going to implement a user control which will validate a TextBox based on custom rules.

Introduction:

Windows Presentation Foundation allows the developers to create attractive Windows applications by provided styles, graphics etc. One the major pain points of using WPF is that it lacks the validation framework. Microsoft WPF team did not put much effort in creating a reliable validation system. In this article we are going to implement a user control which will validate a TextBox based on custom rules.

Implementing a User Control:

The first thing that you need to do is to add a user control to your window. Let's take a look at the XAML code for the user control.



The UserControl defined in the above code is a very simple control.  It consists of a DockPanel control which contains a Label control. The Label will be used to display the error message to the user.  Now let's check out the code behind for UserControl.



The create controls method is used to initialize the TextBox control. The validation rules is an observable collection of a validation rule type.  The validation rules collection contains all the validation rules applied to the TextBox input. Now, let's check out the code for the data context changed event where the validation rules are injected into the binding object.



The binding object has a validation tools collection which is used to hold the custom validation rules. Finally, the binding is set to the TextBox control which is added to the DockPanel control.

Our mission is to validate the TextBox on lost focus event hence, we are going to implement the TextBox lost focus event.



Inside the lost focus event we are extracting the binding expression from the TextBox control and updating the source. The UpdateSource method of the binding expression will lead to firing the validation rules.

This custom user control only consist of only the basic features and can easily be extended to add more rich features. Let's check out the details of how to implement the validation rules.

Implementing Validation Rules:

Validation rules are very easy to implement since you only have to inherit from the validationRule class as shown below:

 

The code above validates the user input against the null and empty values. The validation is only successful if the value is not null and empty. Let's check out another validation rule which forces a developer too type a numeric input into the TextBox.

 

The above code will only validate successfully for numeric values. Now let's implement a form which will make use of the custom requires TextBox UserControl and validate against custom validation rules.

Creating a Window to Test Our UserControl:

Our custom window will validate the FirstName and the LastName properties of the Customer object. First, check out the code below which will bind a new Custom object to the DataGridView gvAddCustomer.



The AddCustomerWindow is shown below:



The custom TextBox XAML code is shown below:



As, you can see in the XAML code above the custom RequiredTextbox contains two validation rules. Both the rules will be triggered on the LostFocus event. Below you can see the screenshot of the validation rules in action which you tab of the TextBox control without entering any value.



The validation style is defined in the App.XAML file which can be adjusted easily. Now, it becomes much easier to apply validation to the TextBox controls. We can also easily customize and add new rules by inherting from the ValidationRule class. This gives us an extensive framework where we can add multiple validation rules easily without having to change our application.

Conclusion:

In this article we demonstrated how to create a WPF UserControl which extends the functionality of a normal TextBox to add validation support. Using the UserControl we can easily add multiple validation rules to our TextBox control. In the next article we will demonstrate how to create a ValidationSummary control which can display the failed validations in the form of a list.

[Download Sample]