Reputation: 4044
Yesterday, I've asked a question about validation in MVVM and someone replied with a piece of code: https://stackoverflow.com/a/13387724/
I'm trying to understand this code, but I just don't understand the indexer part. Can someone explain to me how that code works? When exactly is the get/set called when using IDataErrorInfo and why does he return this[columnName] in the get-part?
Thanks
Upvotes: 4
Views: 444
Reputation:
His code is buggy as I write this. The get
ter would throw a StackOverflowException if called.
When a Binding is configured to perform validation
<TextBox Text="{Binding Hurr, ValidatesOnDataErrors=true}" />
the binding system will, if the data source object implements IDataErrorInfo, use the two methods of that interface to perform validation.
The indexer of the interface takes a string which is the name of the property to validate, and returns a string which is the validation 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 particular code sample is handling validation in the getter/setter kind of odd. Its not a universal example of how implementing IDataErrorInfo
should be, but more likely a snip out of his own personal code with his own personal touches. Most people have their own way of implementing it, but 9/10 times its going to be a switch
statement with the property names all being individual case
s.
Upvotes: 2