简体   繁体   English

如何从另一个类的一个类的属性更改触发事件?

[英]how to trigger an event from a property change in one class in another class?

i have the following class structure and im trying to trigger an event in class when theres a change in a property in another class. 我具有以下类结构,并且当另一个类的属性发生更改时,我试图在类中触发事件。 the prop property in class C is bound to a checkbox element so every time a user ticks or unticks that box - so when it changes from true to false or vice versa - i want to deliver that message to class A so it can run MethodTriggeredByChangeinProp. C类中的prop属性绑定到一个checkbox元素,因此每次用户勾选或取消选中该框时-因此当它从true变为false或反之亦然时-我想将该消息传递给A类,以便它可以运行MethodTriggeredByChangeinProp。 I need class A to know which instance of class B and also which instance of class C in B is triggering that method. 我需要A类来知道B类的哪个实例,以及B中C类的哪个实例正在触发该方法。 the only thing i can think of is making listC an observablecollection and adding the propertychanged event in class B but then i get confused about where to go from there and liking it to class A. any help is appreciated! 我唯一能想到的就是使listC成为可观察的集合,并在B类中添加propertychanged事件,但是随后我对从那里去然后喜欢它到A类感到迷惑。不胜感激! thanks. 谢谢。

public class A{
    List<B> listB;

    void MethodTriggeredByChangeinProp(){
    }
}

public class B{
    List<C> listC;
}

public class C{
    public bool Prop{ get; set; }
}

This can be accomplished with just INotifyPropertyChanged and keeping track of "owners". 只需使用INotifyPropertyChanged并跟踪“所有者”即可完成此操作。

Class C will keep track of property changes, but will accept a B owner and an A notifyMe objects on construction: C类将跟踪属性更改,但将在构造上接受B ownerA notifyMe对象:

    public class C : INotifyPropertyChanged
    {
        public C(B owner, A notifyMe)
        {
            Owner = owner;
            PropertyChanged += notifyMe.MethodTriggeredByChangeinProp;
        }

        public B Owner { get; private set; }

        private bool prop;
        public bool Prop
        {
            get => prop;
            set
            {
                prop = value;
                NotifyPropertyChanged();
            }
        }

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        public event PropertyChangedEventHandler PropertyChanged;
    }

Class B doesn't actually need to be anything special, unless you want to know when an object of Class C is added to your list - then you need the ObservableCollection setup; B类实际上不需要特殊,除非您想知道何时将C类对象添加到列表中-然后您需要ObservableCollection设置; otherwise, List<C> is fine. 否则, List<C>可以。

Class A's MethodTriggeredByChangeinProp changes slightly: A类的MethodTriggeredByChangeinProp稍有变化:

    public class A
    {
        public List<B> listB;

        public void MethodTriggeredByChangeinProp(object sender, PropertyChangedEventArgs e)
        {
            if(sender is C responsibleC)
            {
                var responsibleB = responsibleC.Owner;
                var changedProp = e.PropertyName;
            }
        }

You now know the responsible instances of class B and class C in A whenever a property changes in C. 现在,只要C中的属性发生更改,您就知道A中B类和C类的负责实例。

You may also want to take a look at the Observer Pattern in .NET for a more in-depth configuration. 您可能还想看看.NET中的观察者模式,以进行更深入的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM