CollectionChanged Event

本文关键字:Event CollectionChanged | 更新日期: 2023-09-27 18:25:04

ObservableCollection在每次添加、删除或更新集合中的项目时都会提供CollectionChanged事件,但它会引发单个项目更改的事件,例如,每当我将新项目添加到集合时,它都会引发集合。

现在,我想要的是在完成对集合的所有修改后引发事件,例如,我需要添加2个项目,删除一个项目,更新2个项目。CollectionChanged事件应仅在完成所有这些添加、删除和更新之后激发。

或者,假设我有一个经过所有修改的新集合,现在,我想在分配新集合时引发CollectionChanged,例如:

ObservableCollection<string> mainCollection; //assume it has some items 
mainCollection = updatedCollection; // at this point I want to raise the event.

请提供您的宝贵建议。

问候,

BHavik

CollectionChanged Event

看起来您更愿意关注对mainCollection变量的赋值,而不是启动ObservableCollection类的事件。类似这样的东西:

        private ObservableCollection<MyItemType> _mainCollection;
        public ObservableCollection<MyItemType> MainCollection
        {
            get
            {
                return _mainCollection;
            }
            set
            {
                _mainCollection = value;
                TriggerMyEvent(); // do whatever you like here
            }
        }

它不会按照您编写的方式工作,原因是您订阅了mainCollection对象的事件,然后用另一个对象替换它,只引用相同的变量名。您不需要分配集合,而是将更新集合的所有元素添加到主集合

编辑:ObservableCollection的AddRange:

using System.Collections.Specialized;
using System.Collections.Generic;
namespace System.Collections.ObjectModel
{
/// <summary> 
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. 
/// </summary> 
/// <typeparam name="T"></typeparam> 
public class ObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
{
    /// <summary> 
    /// Adds the elements of the specified collection to the end of the ObservableCollection(Of T). 
    /// </summary> 
    public void AddRange(IEnumerable<T> collection)
    {
        foreach (var i in collection) Items.Add(i);
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add), collection.ToList());
    }
    /// <summary> 
    /// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T). 
    /// </summary> 
    public void RemoveRange(IEnumerable<T> collection)
    {
        foreach (var i in collection) Items.Remove(i);
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove), collection.ToList());
    }
    /// <summary> 
    /// Clears the current collection and replaces it with the specified item. 
    /// </summary> 
    public void Replace(T item)
    {
        ReplaceRange(new T[] { item });
    }
    /// <summary> 
    /// Clears the current collection and replaces it with the specified collection. 
    /// </summary> 
    public void ReplaceRange(IEnumerable<T> collection)
    {
        List<T> old = new List<T>(Items);
        Items.Clear();
        foreach (var i in collection) Items.Add(i);
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace));
    }
    /// <summary> 
    /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class. 
    /// </summary> 
    public ObservableCollection()
        : base() { }
    /// <summary> 
    /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection. 
    /// </summary> 
    /// <param name="collection">collection: The collection from which the elements are copied.</param> 
    /// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception> 
    public ObservableCollection(IEnumerable<T> collection)
        : base(collection) { }
}
}

取自这个问题

标准ObservableCollection<T>不支持此场景。您可以使用所需的逻辑创建一个实现INotifyCollectionChanged接口的类,例如,使用一个方法用另一个集合替换所有项。

这可能对UI不利,例如,如果选择或聚焦了绑定到集合的元素,则如果完全重新初始化集合,则此状态将丢失。这取决于你的情景。