What's the order of execution in property setters when using IDataErrorInfo?

Posted by Benny Jobigan on Stack Overflow See other posts from Stack Overflow or by Benny Jobigan
Published on 2010-04-16T04:17:24Z Indexed on 2010/04/16 10:33 UTC
Read the original article Hit count: 269

Filed under:
|
|
|
|

Situation: Many times with WPF, we use INotifyPropertyChanged and IDataErrorInfo to enable binding and validation on our data objects. I've got a lot of properties that look like this:

public SomeObject SomeData
{
    get { return _SomeData; }
    set { _SomeData = value; OnPropertyChanged("SomeData"); }
}

Of course, I have an appropriate overridden IDataErrorInfo.this[] in my class to do validation.

Question: In a binding situation, when does the validation code get executed? When is the property set? When is the setter code executed? What if the validation fails?

For example:

  1. User enters new data.
  2. Binding writes data to property.
  3. Property set method is executed.
  4. Binding checks this[] for validation.
  5. If the data is invalid, the binding sets the property back to the old value.
  6. Property set method is executed again.

This is important if you are adding "hooks" into the set method, like:

public string PathToFile
{
    get { return _PathToFile; }
    set
    {
        if (_PathToFile != value &&      // prevent unnecessary actions
            OnPathToFileChanging(value)) // allow subclasses to do something or stop the setter
        {
            _PathToFile = value;
            OnPathToFileChanged();  // allow subclasses to do something afterwards
            OnPropertyChanged("PathToFile");
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about properties