更多详细信息 - 当嵌套实现 INotifyPropertyChanged 的属性时,父对象必须传播更改吗?
我的问题几乎类似于以下现有问题: 当嵌套实现 INotifyPropertyChanged 的属性时,父对象必须传播更改?
我的问题是如果我有如下三个级别 人 接触 地址
public class Address : INotifyPropertyChanged
{
string m_city;
public string City
{
get { return m_city; }
set
{
m_city = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("City"));
}
}
}
public class Contact : INotifyPropertyChanged
{
Address m_address;
public Address Address
{
get { return m_address = value; }
set
{
m_address = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("Address"));
}
}
}
public class Person : INotifyPropertyChanged
{
Contact m_contact;
public Contact ContactInfo
{
get { return m_contact = value; }
set
{
m_contact = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("ContactInfo"));
}
}
}
我有一个包含联系人用户控件的人员用户控件。 当我更改城市时,它会在地址类中调用城市属性的notifyPropertychanged。和它 既不调用 Contact 类下的 Address setter,也不调用 Person 类下的 Contact setter。 当城市属性发生变化时,如何通知 person 类???
My question is almost like the following existing question:
When nesting properties that implement INotifyPropertyChanged must the parent object propogate changes?
My question is If I have three levels as follows
Person
Contact
Address
public class Address : INotifyPropertyChanged
{
string m_city;
public string City
{
get { return m_city; }
set
{
m_city = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("City"));
}
}
}
public class Contact : INotifyPropertyChanged
{
Address m_address;
public Address Address
{
get { return m_address = value; }
set
{
m_address = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("Address"));
}
}
}
public class Person : INotifyPropertyChanged
{
Contact m_contact;
public Contact ContactInfo
{
get { return m_contact = value; }
set
{
m_contact = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("ContactInfo"));
}
}
}
I have a person User control that contains a contact user control.
When I change the city it calls notifyPropertychanged of the city property at the address class. and It
doesn't call neither the Address setter under Contact class nor Contact setter under Person class.
How can I notify the person class when the city property changed???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果其他类有兴趣使用该地址,则必须注册该地址的
PropertyChanged
事件。例如,
Contact
只会引发PropertyChanged
如果对象引用发生更改,例如设置Address = new Address()
或Address = null
,则在Address
上发生事件。它不关心地址的属性,这是正确的。如果您希望它关心属性,请在Address.PropertyChanged
上挂接一个PropertyChange
事件以引发Contact.Address
PropertyChange事件如果您希望事件也冒泡到 Person 类,则必须对 Person 类执行相同的操作。
另一种选择是使用某种消息传递系统,例如 MVVM Light 的
Messenger
或 Microsoft Prism 的EventAggregator
。使用这个系统,只要City
发生变化,Address
类就会广播一个事件,而Person
类会订阅这些消息并用任何方式处理它们您希望提供发送消息的Address
等于Person.Contact.Address
The other classes will have to register to the Address's
PropertyChanged
event if they're interested in using itFor example,
Contact
will only raise aPropertyChanged
event onAddress
if the object reference gets changed, such as settingAddress = new Address()
orAddress = null
. It doesn't care about the Address's properties, and rightly so. If you want it to care about the properties, hook up aPropertyChange
event onAddress.PropertyChanged
to raise theContact.Address
PropertyChange eventYou'd have to do the same thing with the Person class if you want the event to bubble up to the Person class as well.
Another alternative is to use some kind of messaging system, such as MVVM Light's
Messenger
, or Microsoft Prism'sEventAggregator
. With this system, theAddress
class would broadcast an event wheneverCity
changes, and thePerson
class would subscribe to these messages and handle them with whatever you want providing theAddress
that sent the message was equal toPerson.Contact.Address