IDataErrorInfo - how it works

Yesterday I asked a question about validation in MVVM, and someone answered with a piece of code: https://stackoverflow.com/a/464618/

I am trying to understand this code, but I just do not understand the part of the indexer. Can someone explain to me how this code works? When exactly get / set is called when using IDataErrorInfo and why does it return this [columnName] in get-part?

thanks

+4
source share
1 answer

His code is buggy when I write this. get ter will throw a StackOverflowException if thrown.

When the binding is configured to perform validation

 <TextBox Text="{Binding Hurr, ValidatesOnDataErrors=true}" /> 

the binding system will be, if the data source object implements IDataErrorInfo , use two methods of this interface to perform validation.

The interface indexer takes a string, which is the name of the property to check, and returns a string, which is a check error, if any, of the current value of the property.

An example of this might be ...

 var pet = new Pet(); var error = pet["Name"]; //"Your pet has no name!" pet.Name = "Fido"; error = pet["Name"]; //"Come on, how unoriginal is that?" 

His concrete code example handles the validation in the type getter / setter odd. This is not a universal example of how to implement IDataErrorInfo , but rather, derive your personal touches from your personal code. Most people have their own way of implementing it, but 9-10 times it will be a switch with property names that are individual case s.

+2
source

Source: https://habr.com/ru/post/1446172/


All Articles