更多详细信息 - 当嵌套实现 INotifyPropertyChanged 的​​属性时,父对象必须传播更改吗?

发布于 2024-12-22 08:17:34 字数 1472 浏览 4 评论 0原文

我的问题几乎类似于以下现有问题: 当嵌套实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2024-12-29 08:17:34

如果其他类有兴趣使用该地址,则必须注册该地址的 PropertyChanged 事件。

例如,Contact 只会引发 PropertyChanged如果对象引用发生更改,例如设置 Address = new Address()Address = null,则在 Address 上发生事件。它不关心地址的属性,这是正确的。如果您希望它关心属性,请在 Address.PropertyChanged 上挂接一个 PropertyChange 事件以引发 Contact.Address PropertyChange

public class Contact : INotifyPropertyChanged
{
    Address m_address;

    public Address Address
    {
        get { return m_address = value; }
        set
        {
            // Remove old PropertyChanged notification if it existed
            if (m_address != null)
                m_address.PropertyChanged -= Address_PropertyChanged;

            m_address = value;

            // Add new PropertyChanged notification if it existed
            if (m_address != null)
                m_address.PropertyChanged += Address_PropertyChanged;

            NotifyPropertyChanged(new PropertyChangedEventArgs("Address"));
        }
    }
}


void Address_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "City")
        NotifyPropertyChanged(new PropertyChangedEventArgs("Address"));
}

事件如果您希望事件也冒泡到 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 it

For example, Contact will only raise a PropertyChanged event on Address if the object reference gets changed, such as setting Address = new Address() or Address = null. It doesn't care about the Address's properties, and rightly so. If you want it to care about the properties, hook up a PropertyChange event on Address.PropertyChanged to raise the Contact.Address PropertyChange event

public class Contact : INotifyPropertyChanged
{
    Address m_address;

    public Address Address
    {
        get { return m_address = value; }
        set
        {
            // Remove old PropertyChanged notification if it existed
            if (m_address != null)
                m_address.PropertyChanged -= Address_PropertyChanged;

            m_address = value;

            // Add new PropertyChanged notification if it existed
            if (m_address != null)
                m_address.PropertyChanged += Address_PropertyChanged;

            NotifyPropertyChanged(new PropertyChangedEventArgs("Address"));
        }
    }
}


void Address_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "City")
        NotifyPropertyChanged(new PropertyChangedEventArgs("Address"));
}

You'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's EventAggregator. With this system, the Address class would broadcast an event whenever City changes, and the Person class would subscribe to these messages and handle them with whatever you want providing the Address that sent the message was equal to Person.Contact.Address

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文