Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Raise PropertyChanged when an inner object property got changed

I got a scenario like this

Class Parent 
{
    Property  A;
 }

 Class A 
 {
      Property X
 }

How can I get a PropertyChangedNotification on Property A when X changes? I don’t want to refer ‘Parent’ in class A or any kind of event which spoils my decoupling. What I basically want is to make the Parent.IsDirty==true. This is a very simplified version of my story, I got tens of classes like Parent, so I am looking for some generic way to handle this.

Please note that this is not the actual code. I got all INotifyPropertyChanged implementation. I am just wondering any easy mechanism like RaisePropertyChanged("A.X")

like image 800
Jobi Joy Avatar asked Mar 11 '10 22:03

Jobi Joy


1 Answers

You can try to register the propertychanged event in the parent class. In the constructor you can subribe to the event:

public Parent()
{
    A.OnPropertyChanged += OnAPropertyChanged;
}

void OnAPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "X")
        if(PropertyChanged != null)
             PropertyChanged(this, new PropertyChangedEventArgs("A"))
}

hope this helps...

like image 75
RoelF Avatar answered Sep 20 '22 10:09

RoelF