简体   繁体   English

当另一个对象的属性发生更改时,通知一个对象

[英]Notify One object when a property of another object changes

I have a parent object called Page that has a List of objects called Control: 我有一个名为Page的父对象,它有一个名为Control的对象列表:

public class Page
{
   List<CustomControl> controls {get;set;}
}

The CustomControl class has the following defintion: CustomControl类具有以下定义:

public class CustomControl
{
 string Name {get;set;}
 string Value {get;set;}
}

Say for instance the Page class has two CustomControls A and B. Is it possible to Notify Custom Control B when the property Value of Custom Control A changes so that it can change some of its properties. 比方说,Page类有两个CustomControls A和B.当Custom Control A的属性Value发生变化时,可以通知自定义控件B,以便它可以改变它的一些属性。

I was thinking of implementing the INotifyPropertyChanged event on the CustomControl class now how do I notify an instance of the CustomControl when another instance of the same class has some property of its Modified. 我正在考虑在CustomControl类上实现INotifyPropertyChanged事件,现在当同一个类的另一个实例具有其Modified的某个属性时,如何通知CustomControl的实例。

public class CustomControl : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value) return;
            _name = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }

    private string _value;
    public string Value
    {
        get { return _value; }
        set
        {
            if (_value == value) return;

            _value = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Value"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    internal virtual void OnSiblingInPagePropertyChanged(object sender, PropertyChangedEventArgs args)
    {

    }
}

public class CustomControlObservableColletion : ObservableCollection<CustomControl>
{
    // Required because, by default, it is not possible to find out which items
    // have been cleared when the CollectionChanged event is fired after a .Clear() call.
    protected override void ClearItems()
    {
        foreach (var item in Items.ToList())
            Remove(item);
    }

}

public class Page
{
    public IList<CustomControl> Controls { get; private set; }

    public Page()
    {
        var controls = new CustomControlObservableColletion();
        controls.CollectionChanged += OnCollectionChanged;
        Controls = controls;
    }

    private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                RegisterControls(e.NewItems);
                break;

            case NotifyCollectionChangedAction.Replace:
                RegisterControls(e.NewItems);
                DeRegisterControls(e.OldItems);
                break;

            case NotifyCollectionChangedAction.Remove:
                DeRegisterControls(e.OldItems);
                break;
        }
    }

    private void RegisterControls(IList controls)
    {
        foreach (CustomControl control in controls)
            control.PropertyChanged += OnControlPropertyChanged;
    }

    private void DeRegisterControls(IList controls)
    {
        foreach (CustomControl control in controls)
            control.PropertyChanged -= OnControlPropertyChanged;
    }

    private void OnControlPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        foreach (var control in Controls.Where(c => c != sender))
            control.OnSiblingInPagePropertyChanged(sender, e);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PropertyNotificatioDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            PropertyNotification notification = new PropertyNotification();   //Create an object of PropertyNotification class.

            //Create EmployeeValueChange handler.

            PropertyNotification.EmployeeValueChange += new PropertyNotification.EmployeeNameHandler(PropertyNotification_EmployeeValueChange);



            //Display a message.

            Console.Write("Enter Value  :  ");

            //Read a value and initilize it in property.

            notification.EmployeeName = Console.ReadLine();

        }



        //Handler for property notification is created.

        static void PropertyNotification_EmployeeValueChange(object sender, EventArgs e)

        {

            Console.WriteLine("Employee name is changed."+sender);

        }

    }



    public class PropertyNotification

    {

        //Create a private variable which store value.

        private static string _employeeName;

        //Create a delegate of named EmployeeNamed=Handler

        public delegate void EmployeeNameHandler(object sender, EventArgs e);



        //Create a event variable of EmployeeNameHandler

        public static event EmployeeNameHandler EmployeeValueChange;



        //Create a static method named OnEmployeeNameChanged

        public static void OnEmployeeNameChanged(EventArgs e)

        {

            if (EmployeeValueChange != null)

                EmployeeValueChange(_employeeName, e);

        }



        //Create a property EmployeeName

        public string EmployeeName

        {

            get

            {

                return _employeeName;

            }

            set

            {

                //Check if value of property is not same.

                if (_employeeName != value)

                {

                    OnEmployeeNameChanged(new EventArgs());

                    _employeeName = value;

                }

            }

        }

    }

}

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

相关问题 当 ObservableCollection 中的 object 属性发生变化时,通知另一个属性 CommunityToolkit Mvvm - When object property in ObservableCollection changed, notify another property, CommunityToolkit Mvvm 模拟另一个对象上一个对象的属性/列表更改 - Mimic property/list changes on an object on another object 将一个对象设置为另一个对象 C# 时如何影响原始对象的属性 - How to affect property of original object when setting one object equal to another object C# 防止对一个 window 上的 object 的更改影响另一个 - Prevent changes to an object on one window affecting another 对象的属性更改时执行功能 - Execute a function when an object's property changes WPF:绑定到对象的属性;当对象改变时会发生什么? - WPF: bind to an object's property; what happens when the object changes? 如何编写lambda以根据对象中的另一个属性获取一个属性 - How to write a lambda to get one property based on another property in an object 在基础模型数据更改时,通知ViewModel中定义的属性更改 - Notify property change defined in the ViewModel when underlying Model data changes 绑定对象的属性更改时,WPF Caliburn Micro CanExecute - WPF Caliburn Micro CanExecute when property of binded object changes 绑定到对象本身,当单个属性更改时不会更新 - Binding to object itself, doesn't update when individual property changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM