7

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")

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119

2 Answers2

8

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...

RoelF
  • 7,483
  • 5
  • 44
  • 67
0

I don't know if this is the best practice but: what about something like this:

private Movie movie;
public Movie Movie
{
    get { return movie; }
    set
    {
        var oldProperties = typeof(Movie).GetProperties();
        foreach (var property in oldProperties)
        {
            if (property.GetValue(movie).GetHashCode() != value.GetType().GetProperty(property.Name).GetValue(value).GetHashCode())
            {
                RaisePropertyChanged(property.Name);
            }
        }

        movie = value;

    }
}

surely you can take it outside to an external helper that take the 2 objects and the event handler

armageddon
  • 125
  • 1
  • 10