问题描述
此问题将显示我在实现/使用INotifyPropertyChanged时对预期行为的了解不足:
this question is going to show my lack of understanding of the expected behavior when implementing/using INotifyPropertyChanged:
问题是-用于按预期方式绑定工作您有一个本身实现INotifyPropertyChanged的类,该类具有INotifyPropertyChanged类型的嵌套属性,您是否希望在内部订阅这些属性的更改通知,然后传播这些通知?还是绑定基础设施有望使这些功能变得不必要?
The question is - for binding to work as expected, when you have a class which itself implements INotifyPropertyChanged, that has nested properties of type INotifyPropertyChanged are you expected to internally subscribe to change notification for these properties and then propagate the notifications? Or is the binding infrastructure expected to have the smarts to make this unnecessary?
例如(请注意,此代码并不完整-仅用于说明问题):
For example (note this code is not complete - just meant to illustrate the question):
public class Address : INotifyPropertyChanged
{
string m_street
string m_city;
public string Street
{
get { return m_street; }
set
{
m_street = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("Street"));
}
}
public string City
{
get { return m_city; }
set
{
m_city = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("City"));
}
}
public class Person : INotifyPropertyChanged
{
Address m_address;
public Address
{
get { return m_address = value; }
set
{
m_address = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("Address"));
}
}
}
因此,在此示例中,我们在Person对象中有一个嵌套的Address对象。两者都实现了INotifyPropertyChanged,因此更改其属性将导致向订阅者传输属性更改通知。
So, in this example we've got a nested Address object in a Person object. Both of which implement INotifyPropertyChanged so that alteration of their properties will result in transmission of property change notifications to subscribers.
但是,假设使用绑定功能,有人正在订阅以更改通知Person对象,并且正在监听 Address属性的更改。如果Address属性本身发生更改(分配了一个不同的Address对象),他们将收到通知,但是如果嵌套地址对象(城市或街道)中包含的数据发生了更改,则它们将不会收到通知。
But let's say using binding someone is subscribing to change notification on a Person object, and is 'listening' for changes to the Address property. They will receive notifications if the Address property itself changes (a different Address object is assigned) but WILL NOT receive notifications if the data contained by the nested address object (the city, or street) are changed.
这会导致一个问题-绑定基础结构是否可以处理此问题,还是我应该在Person的实现中订阅更改地址对象上的通知,然后将其作为 Address的更改进行传播?
This leads to the question - is the binding infrastructure expected to handle this, or should I within my implementation of Person be subscribing to change notifications on the address object and then propagating them as changes to "Address"?
如果您到此为止,感谢您花时间阅读这个冗长的问题吗?
If you get to this point, thanks for just taking the time in reading this long winded question?
推荐答案
最简单的方法之一是向Person中添加一个事件处理程序,该处理程序将处理来自m_address对象的通知事件:
One of the simplest ways to do it is to add an event handler to Person which will handle notification events from m_address object:
public class Person : INotifyPropertyChanged
{
Address m_address;
public Address
{
get { return m_address = value; }
set
{
m_address = value;
NotifyPropertyChanged(new PropertyChangedEventArgs("Address"));
m_address.PropertyChanged += new PropertyChangedEventHandler( AddressPropertyChanged );
}
}
void AddressPropertyChanged( object sender, PropertyChangedEventArgs e )
{
NotifyPropertyChanged(new PropertyChangedEventArgs("Address"))
}
}
这篇关于当嵌套实现INotifyPropertyChanged的属性时,父对象必须传播更改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!