In the first post I looked at how you wire-up UI controls to a model in the absence of a databinding framework.I showed how databindings can be created in code-behind, removing the need for the various event handlers that the manual method requires, resulting in more readable code, where the connection between a UI control and a model property is all in one place.The Label will be used to display the error message to the user.Now let's check out the code behind for User Control.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 Text Box based on custom rules.The binding object has a validation tools collection which is used to hold the custom validation rules.
Now let's implement a form which will make use of the custom requires Text Box User Control and validate against custom validation rules.
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.
Below you can see the screenshot of the validation rules in action which you tab of the Text Box control without entering any value. We can also easily customize and add new rules by inherting from the Validation Rule class.
This gives us an extensive framework where we can add multiple validation rules easily without having to change our application.
Our mission is to validate the Text Box on lost focus event hence, we are going to implement the Text Box lost focus event.