Open sandboxFocusImprove this doc

Implementing INotifyPropertyChanging

By convention, the PropertyChanged event must be raised after the property value has changed. However, some components need to be signaled before the property value will be changed. This is the role of the INotifyPropertyChanging interface.

Because the INotifyPropertyChanging interface is not portable (in Xamarin, it is even a part of a different namespace), PostSharp cannot introduce it. However, if you implement the INotifyPropertyChanging interface yourself in your code, PostSharp will signal the PropertyChanging event. To make that work, you need to create an OnPropertyChanging method with the right signature.

To add the INotifyPropertyChanging interface to a class:

  1. Make your class implement INotifyPropertyChanging and add the PropertyChanging event.

  2. Add the OnPropertyChanging method with exactly the following signature, and invoke the PropertyChanging event.

    protected void OnPropertyChanging( string propertyName )
    {
        if ( this.PropertyChanging != null )
        {
          this.PropertyChanging( this, new PropertyChangingEventArgs ( propertyName ) );
        }
    }
    
  3. Add the NotifyPropertyChangedAttribute aspect to your class as described in INotifyPropertyChanged.

Note

The contract between your class and the NotifyPropertyChangedAttribute is only the OnPropertyChanging method. As long as this method exists in the class, it will be invoked by the aspect before the value of a property changes.

Example

The following example demonstrates how to implement INotifyPropertyChanging.

[NotifyPropertyChanged]                
public class MyClass : INotifyPropertyChanging
{
    public event PropertyChangingEventHandler PropertyChanging;
    
    protected void OnPropertyChanging( string propertyName )
    {
       if ( this.PropertyChanging != null )
       {
          this.PropertyChanging( this, new PropertyChangingEventArgs ( propertyName ) );
       }
    }
    
    public string MyProperty { get; set; }
}