2012-02-20 100 views
0

ObservableCollection提供CollectionChanged事件我们添加,删除或更新集合中的项目,但它引发了单个项目更改的事件,例如,每当将新项目添加到集合时,它将引发集合。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

回答

1

看起来你宁愿看着赋值给变量mainCollection而不是关火的ObservableCollection类的事件。事情是这样的:

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

我不想提出我自己的事件,我只想使用ObservableColleciton的CollectionChangedEvent。 – 2012-02-20 05:20:21

+0

@RaoBHavik:你所要求的是不可能的。 – 2012-02-20 05:26:21

+0

我可以选择其他选项而不是ObservableColleciton它可以满足我的要求。 – 2012-02-20 05:26:26

2

它不会工作,你写的方式,原因是您订阅mainCollection对象的事件,然后用整个另一个对象只是相同的变量名referencng它替换它。你不必指定集合,但以更新的集合中的所有元素添加到主集合

编辑: 的AddRange的的ObservableCollection:

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) { } 
} 
} 

this question

+0

在这种情况下,它会为每个我不想发生的添加语句引发CollectionChanged。 – 2012-02-20 06:26:16

+0

@RaoBHavik,我编辑了我的答案,AddRange方法调用的CollectionChange只有一次 – 2012-02-20 06:32:05

1

标准采取ObservableCollection<T>不支持这种情况。您可以使用您需要的逻辑创建一个实现INotifyCollectionChanged接口的类,例如,使用一种方法将所有项目替换为另一个集合。

这可能对UI没有好处,例如,如果选择了集合绑定元素或将焦点集中,则如果完全重新初始化集合,则此状态将会丢失。取决于你的情况。

+0

我不能在解决方案中添加我自己的集合。 – 2012-02-20 06:27:51

相关问题