将NotifyPropertyChanged路由到另一个类 [英] Route NotifyPropertyChanged to another class

查看:80
本文介绍了将NotifyPropertyChanged路由到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有两个类A和B.都实现INotifyPropertyChanged.
B类的属性为X,而A类的属性为B.
当X改变时,NotifyPropertyChanged在类B中触发.如何知道类A,当X具有类B的属性时,它已经改变了?有没有办法将该事件提升到A类?

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

推荐答案

您是否在理解我所说的内容时遇到困难,还是只是根据自己的理解来重新措辞?是的,您可以将事件冒泡.当A创建B的实例时,它可以订阅B的PropertyChanged事件,以便在类A的类B的实例具有更改的属性时通知类A.当它收到有关更改的属性的通知时,它随后可以随后自己发出有关ClassB属性已更改的通知.流程如下:
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. 类B的实例将发送属性"X"已更改的通知.
  2. 将类A的实例通知属性"X"的更改.
  3. 根据通知,类A会发出有关属性"ClassB"已更改的通知.


我希望这是有道理的.


I hope that makes sense.


INotifyPropertyChanged要求该类具有一个事件,我认为该事件称为PropertyChanged.订阅该事件以在子属性更改时得到通知.然后,在PropertyChanged的处理程序中,可以在更高级别的类中触发PropertyChanged.
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.


是的,这是所有类都实现的功能:
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));
        }


因此,当X更改时,将调用类B的OnPropertyChanged并触发PropertyChanged事件.但是更高级别的A类对此变化一无所知.

我想那是因为B类的属性发生了变化,但B类本身却没有改变为A类.


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.


这篇关于将NotifyPropertyChanged路由到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆