Share via

How to: Implement Validation Logic on Custom Objects

This example shows how to implement validation logic on a custom object and then bind to it.


You can provide validation logic on the business layer if your source object implements IDataErrorInfo, as in the following example, which defines a Person object that implements IDataErrorInfo:

public class Person : IDataErrorInfo
    private int age;

    public int Age
        get { return age; }
        set { age = value; }

    public string Error
            return null;

    public string this[string name]
            string result = null;

            if (name == "Age")
                if (this.age < 0 || this.age > 150)
                    result = "Age must not be less than 0 or greater than 150.";
            return result;
Public Class Person
    Implements IDataErrorInfo

    Private _age As Integer
    Public Property Age() As Integer
            Return _age
        End Get
        Set(ByVal value As Integer)
            _age = value
        End Set
    End Property

    Public ReadOnly Property [Error]() As String Implements IDataErrorInfo.Error
            Return Nothing
        End Get
    End Property

    Default Public ReadOnly Property Item(ByVal columnName As String) As String Implements IDataErrorInfo.Item
            Dim result As String = Nothing

            If columnName = "Age" Then
                If Me._age < 0 OrElse Me._age > 150 Then
                    result = "Age must not be less than 0 or greater than 150."
                End If
            End If
            Return result
        End Get
    End Property
End Class

In the following example, the text property of the text box binds to the Person.Age property, which has been made available for binding through a resource declaration that is given the x:Key data. The DataErrorValidationRule checks for the validation errors raised by the IDataErrorInfo implementation.

<Window x:Class="BusinessLayerValidation.Window1"
        Title="WPF IDataErrorInfo Sample" Width="350" Height="150"
        <src:Person x:Key="data"/>

        <!--The tool tip for the TextBox to display the validation error message.-->
        <Style x:Key="textBoxInError" TargetType="TextBox">
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self},

    <StackPanel Margin="20">
        <TextBlock>Enter your age:</TextBlock>

        <TextBox Style="{StaticResource textBoxInError}">
                <!--By setting ValidatesOnExceptions to True, it checks for exceptions
                that are thrown during the update of the source property.
                An alternative syntax is to add <ExceptionValidationRule/> within
                the <Binding.ValidationRules> section.-->
                <Binding Path="Age" Source="{StaticResource data}"
                        <!--DataErrorValidationRule checks for validation 
                            errors raised by the IDataErrorInfo object.-->
                        <!--Alternatively, you can set ValidationOnDataErrors="True" on the Binding.-->

        <TextBlock>Mouse-over to see the validation error message.</TextBlock>

Alternatively, instead of using the DataErrorValidationRule, you can set the ValidatesOnDataErrors property to true.

See also