Search code examples
c#mvvmpostsharp

How to catch property changing using Postsharp?


I have a viewmodel that is tagged with [NotifyPropertyChanged]. The properties are of course bound on input controls, like textboxes. I need to know, that the model's property was changed because of an input.

How can I catch this event?


Solution

  • If a class decorated by NotifyPropertyChanged implements INotifyPropertyChanged directly, then PostSharp requires that there is a method with signature:

    void OnPropertyChanged(string propertyName)
    

    This method has to raise PropertyChanged event explicitly. Working example could look like this:

    [NotifyPropertyChanged]
    public class OsModel : INotifyPropertyChanged
    {
        public int P1 { get; set; }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    Additional information could be found here.