简体   繁体   English

知道集合中的变量是否已更改

[英]Know if variable in collection has changed

I have ObservableCollection that contains class: MyClass . 我有ObservableCollection包含类: MyClass

    private ObservableCollection<MyClass> _myCollection;

    public ObservableCollection<MyClass> MyCollection
    {
        get { return _myCollection; }
        set
        {
            _myCollection= value;
        }
    }

I want to know in C# code, if the collection changed. 我想用C#代码知道集合是否更改。

So I registered to event of the collection: CollectionChanged it works when I add / delete a record. 因此,我注册了该集合的事件: CollectionChanged ,当我添加/删除记录时它可以工作。

Here the registred: 在这里注册:

   MyCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(MyCollection_CollectionChanged);

I want this event to work (or register another event) when something changes within the class. 当班级中发生某些更改时,我希望此事件能够工作(或注册另一个事件)。

At first I thought to register for PropertyChanged event of the class, then through it run the event CollectionChanged - it seems complicated and unnecessary. 起初,我想注册该类的PropertyChanged事件,然后通过它运行事件CollectionChanged似乎很复杂且不必要。

I think that as the binding of wpf can recognize a change in the class and a change in the collection itself, then it is possible to do this through code. 我认为,由于wpfbinding可以识别类中的更改和集合本身中的更改,因此可以通过代码来实现。

Am I wrong? 我错了吗? (More precise question is: Does anyone know a way to this?) (更精确的问题是:有人知道这种方法吗?)

As already stated by Nik, the PropertyChanged event is the right way to go. 如Nik所述,PropertyChanged事件是正确的方法。 But I think you shouldn't invoke the CollectionChanged event manually or "force" it that way. 但是我认为您不应该手动调用CollectionChanged事件或以这种方式“强制”它。 You should just refactor your code inside the event, like this: 您应该只在事件内部重构代码,如下所示:

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class Class1
{
    private ObservableCollection<MyClass> _myCollection = new ObservableCollection<MyClass>();
    public ObservableCollection<MyClass> MyCollection
    {
        get { return _myCollection; }
        // set { _myCollection = value; } // see Clemens' comment
    }

    public Class1()
    {
        // hook up this event at the appropriate place, does not have to be the ctor
        MyCollection.CollectionChanged += MyCollection_CollectionChanged;

        MyClass m = new MyClass() { Name = "Example" };

        MyCollection.Add(m); // calls ExecuteOnAnyChangeOfCollection
        m.Name = "ChangedValue"; // calls ExecuteOnAnyChangeOfCollection
    }

    void MyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (MyClass item in e.NewItems)
                item.PropertyChanged += MyClass_PropertyChanged;
        }

        if (e.OldItems != null)
        {
            foreach (MyClass item in e.OldItems)
                item.PropertyChanged -= MyClass_PropertyChanged;
        }

        ExecuteOnAnyChangeOfCollection();
    }

    void MyClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        ExecuteOnAnyChangeOfCollection();
    }

    private void ExecuteOnAnyChangeOfCollection()
    {
        // handling code ...
        System.Windows.MessageBox.Show("Collection has changed.");
    }

You are right. 你是对的。 The easiest way to get notifications from MyClass is to subscribe to PropertyChanged event (if MyClass implements INotifyPropertyChanged). 从MyClass接收通知的最简单方法是订阅PropertyChanged事件(如果MyClass实现INotifyPropertyChanged)。

Then to update UI you can either Remove and then Add the changed item (this will rise CollectionChanged automatically) or invoke CollectionChanged event with Reset parameter manually (you will need to extend ObservableCollection class to do so, but thats a fairly easy task). 然后,要更新UI,您可以删除然后添加更改的项目(这将自动升高CollectionChanged),或者手动使用Reset参数调用CollectionChanged事件(您需要扩展ObservableCollection类来这样做,但这是一个相当简单的任务)。

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

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