Search code examples
wpfmvvmidataerrorinfo

how indexer is called in Idataerrorinfo?


In MVVM IDataErrorInfo is used for validation. In this on which time indexer check value can not get. and after calling it how it check value i can not get.the code is here. what is the value passed in column name.

public string this[string columnName]
{
    get { return GetValidationError(columnName); }
}

why use indexer for validation in wpf .can any one say about it.


Solution

  • If you use the IDataErrorInfo interface, you validate the class that implements the interface.

    If you have a property Name, activate the validation and change the property, the indexer is called with string columnName = "Name". Now you can handle the "Name" validation in an if statement

    if(columnName  == "Name")
        // do Name validation
    

    //edit

    the validation is fired in wpf if you change the property by binding. You should decorate your binding like this:

    Text="{Binding UpdateSourceTrigger=LostFocus, Path=FirstName, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"
    

    Here is a nice and simple explanation of the IDataErrorInfo implementation using WPF databinding and an errortemplate.

    WPF: Validation made easy with IDataErrorInfo