Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have two classes A and B. Both implement INotifyPropertyChanged.
Class B has a property X and Class A has a property of class B.
When X changes, NotifyPropertyChanged fires in class B. How does class A get to know, that X has changed when it has a property of class B? Is there a way to bubble that event up to class A?
public class B : INotifyPropertyChanged
{
	private int x;
	public int X
	{
		get
		{
			return x;
		}
		set
		{
			x = value;
			NotifyPropertyChanged("X"); //this fires
		}
	}
	//Handler and Function for NotifyPropertyChanged not explicitly mentioned....
}

public class A : INotifyPropertyChanged
{
	private B classb;
	public B ClassB
	{
		get
		{
			return classb;
		}
		set
		{
			classb = value;
			NotifyPropertyChanged("ClassB"); //this fires not
		}
	}
	//Handler and Function for NotifyPropertyChanged not explicitly mentioned....
}
Posted

Are you having trouble understanding what I said, or are you just rephrasing it according to your understanding? YES, you can bubble the event up. When A creates an instance of B, it can subscribe to the PropertyChanged event of B so that class A gets notified when its instance of class B has a changed property. When it gets notified of that changed property, it can then subsequently send out a notification itself that the ClassB property was changed. The flow would go like this:
  1. An instance of class B would send a notification that property "X" was changed.
  2. An instance of class A would be notified of the change to property "X".
  3. Upon notification, class A would send out a notification that property "ClassB" was changed.

I hope that makes sense.
 
Share this answer
 
INotifyPropertyChanged requires that the class have an event, which I think is called PropertyChanged. Subscribe to that event to be notified when a sub-property changes. Then, in your handler for PropertyChanged, you can fire PropertyChanged in the higher-level class.
 
Share this answer
 
Yes, this is what all classes implement:
public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

So when X changes, OnPropertyChanged of class B gets called and fires the PropertyChanged event. But the higher level class A doesn't get to know anything about this change.

i guess that is because the property of class B changes but not class B itself as property of class A.
 
Share this answer
 
Thank you for your helpful advice.
Now it works properly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900