如何让父母班的孩子知道孩子的变化? [英] How to let a parent class know about a change in its children?

查看:79
本文介绍了如何让父母班的孩子知道孩子的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是示例代码:

public class MyParent : INotifyPropertyChanged
{
    List<MyChild> MyChildren;

    public bool IsChanged
    {
        get
        {
            foreach (var child in MyChildren)
            {
                if (child.IsChanged) return true;
            }
            return false;
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void RaiseChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
    }
}

public class MyChild : INotifyPropertyChanged
{
    private int _Value;
    public int Value
    {
        get
        {
            return _Value;
        }
        set
        {
            if (_Value == value)
                return;
            _Value = value;
            RaiseChanged("Value");
            RaiseChanged("IsChanged");
        }
    }
    private int _DefaultValue;
    public int DefaultValue
    {
        get
        {
            return _DefaultValue;
        }
        set
        {
            if (_DefaultValue == value)
                return;
            _DefaultValue = value;
            RaiseChanged("DefaultValue");
            RaiseChanged("IsChanged");
        }
    }

    public bool IsChanged
    {
        get
        {
            return (Value != DefaultValue);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaiseChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
    }
}

比方说,我现在有两个类的实例,一个实例为myParent,另一个实例为myChild.我有两个视觉元素,每个视觉元素都有一个绑定到实例的IsChnaged属性的属性;绑定到myParent.IsChanged的ElementA和绑定到myChild.IsChanged的ElementB.

Let's say I now have two instances of my classes, one as myParent, and the other as myChild. I have two visual elements, that each have a property bound to the IsChnaged property of my instances; ElementA bound to myParent.IsChanged and ElementB bound to myChild.IsChanged.

当myChild.Value与其默认值不同时,myChild.IsChanged设置为true,并相应地更新ElementB.

When myChild.Value differs from its default value, the myChild.IsChanged is set to true and the ElementB is updated accordingly.

我需要的是当myParent子项(这里只有一个)中的任何一个将其IsChanged值设置为true,将其自己(父项)的IsChanged值设置为true及其对应的值时元素(此处为ElementA)进行相应的更新.

What I need is when either of the myParent children (which here is only one) have their IsChanged value set to true, its own (the parent's) IsChanged value be set to true and its corresponding element (ElementA here) be updated accordingly.

myParent.IsChanged仅读取一次(设置绑定时),并且对其子项更改没有任何意义.我应该在哪里放置MyParent的RaiseChanged("IsChanged")?我怎么让父母知道孩子何时变了?

The myParent.IsChanged is only read once (when the binding is set) and it has no sense about its children changing. Where should i put the RaiseChanged("IsChanged") for MyParent? How can I let the parent know when its children have changed?

预先感谢

推荐答案

INotifyPropertyChanged 已经为您提供了以下机制: PropertyChanged 事件.只需让父级为其子级的 PropertyChanged 添加一个处理程序,然后在该处理程序中调用 RaiseChanged("IsChanged");

INotifyPropertyChanged has already provided the mechanism for you: the PropertyChanged event. Just have the parent add a handler to its children's PropertyChanged, and then in that handler call RaiseChanged("IsChanged");

此外,您可能希望将 INotifyPropertyChanged 实现放在基类中,并让您的ViewModel(看起来是)继承自该类.当然,此选项不是必需的,但是它将使代码更简洁.

Also, you may want to put the INotifyPropertyChanged implementation in a base class, and have your (what appear to be) ViewModels inherit from that. Not required for this option, of course, but it will make the code a little cleaner.

更新:在父对象中:

// This list tracks the handlers, so you can
// remove them if you're no longer interested in receiving notifications.
//  It can be ommitted if you prefer.
List<EventHandler<PropertyChangedEventArgs>> changedHandlers =
    new List<EventHandler<PropertyChangedEventArgs>>();

// Call this method to add children to the parent
public void AddChild(MyChild newChild)
{
    // Omitted: error checking, and ensuring newChild isn't already in the list
    this.MyChildren.Add(newChild);
    EventHandler<PropertyChangedEventArgs> eh =
        new EventHandler<PropertyChangedEventArgs>(ChildChanged);
    newChild.PropertyChanged += eh;
    this.changedHandlers.Add(eh);
}

public void ChildChanged(object sender, PropertyChangedEventArgs e)
{
    MyChild child = sender as MyChild;
    if (this.MyChildren.Contains(child))
    {
        RaiseChanged("IsChanged");
    }
}

实际上,您不必在子类中添加任何内容,因为它在更改时已经引发了正确的事件.

You don't actually have to add anything to the child class, since it is already raising the correct event when it changes.

这篇关于如何让父母班的孩子知道孩子的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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