C# CollectionChanged事件
C# CollectionChanged事件,c#,observablecollection,C#,Observablecollection,observateCollection在我们从集合中添加、删除或更新项目时提供CollectionChanged事件,但它会引发单个项目更改的事件,例如,每当我向集合中添加新项目时,它就会引发集合 现在,我想要的是在完成对集合的所有修改后引发事件,例如,我需要添加2项、删除1项和更新2项。CollectionChanged事件应仅在完成所有这些添加、删除和更新后触发 或者,假设我有一个包含所有修改的新集合,现在,我想在分配新集合时引发CollectionChanged,例如: Observabl
observateCollection
在我们从集合中添加、删除或更新项目时提供CollectionChanged
事件,但它会引发单个项目更改的事件,例如,每当我向集合中添加新项目时,它就会引发集合
现在,我想要的是在完成对集合的所有修改后引发事件,例如,我需要添加2项、删除1项和更新2项。CollectionChanged
事件应仅在完成所有这些添加、删除和更新后触发
或者,假设我有一个包含所有修改的新集合,现在,我想在分配新集合时引发CollectionChanged
,例如:
ObservableCollection<string> mainCollection; //assume it has some items
mainCollection = updatedCollection; // at this point I want to raise the event.
ObservableCollection主集合//假设它有一些项目
mainCollection=updatedCollection;//在这一点上,我想提出这一事件。
请提供您宝贵的建议
问候,
BHavik看起来您更希望看到对mainCollection变量的赋值,而不是触发ObservableCollection类的事件。大概是这样的:
private ObservableCollection<MyItemType> _mainCollection;
public ObservableCollection<MyItemType> MainCollection
{
get
{
return _mainCollection;
}
set
{
_mainCollection = value;
TriggerMyEvent(); // do whatever you like here
}
}
private observeCollection\u main collection;
公共可观测集合主集合
{
得到
{
返回(主收集);;
}
设置
{
_mainCollection=值;
TriggerMyEvent();//在这里做你想做的事
}
}
看起来您更希望看到对mainCollection变量的赋值,而不是触发ObservableCollection类的事件。大概是这样的:
private ObservableCollection<MyItemType> _mainCollection;
public ObservableCollection<MyItemType> MainCollection
{
get
{
return _mainCollection;
}
set
{
_mainCollection = value;
TriggerMyEvent(); // do whatever you like here
}
}
private observeCollection\u main collection;
公共可观测集合主集合
{
得到
{
返回(主收集);;
}
设置
{
_mainCollection=值;
TriggerMyEvent();//在这里做你想做的事
}
}
它无法按您编写的方式工作,原因是您订阅了mainCollection对象的事件,然后将其替换为另一个对象,只引用了相同的变量名。您不需要分配集合,但需要将更新集合的所有元素添加到主集合
编辑:
可观察收集的添加范围:
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) { }
}
}
使用System.Collections.Specialized;
使用System.Collections.Generic;
命名空间System.Collections.ObjectModel
{
///
///表示动态数据集合,该集合在添加、删除项或刷新整个列表时提供通知。
///
///
公共类ObservableCollection:System.Collections.ObjectModel.ObservableCollection
{
///
///将指定集合的元素添加到ObservableCollection(of T)的末尾。
///
公共void AddRange(IEnumerable集合)
{
foreach(集合中的var i)项。添加(i);
OnCollectionChanged(新建NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add)、collection.ToList());
}
///
///从ObservableCollection(of T)中删除指定集合中每个项的第一次出现。
///
公共无效删除范围(IEnumerable集合)
{
foreach(集合中的var i)项。删除(i);
OnCollectionChanged(新建NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove),collection.ToList());
}
///
///清除当前集合并将其替换为指定项。
///
公共无效替换(T项)
{
替换范围(新的T[]{item});
}
///
///清除当前集合并将其替换为指定集合。
///
公共void ReplaceRange(IEnumerable集合)
{
旧列表=新列表(项目);
Items.Clear();
foreach(集合中的var i)项。添加(i);
OnCollectionChanged(新建NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace));
}
///
///初始化System.Collections.ObjectModel.ObservableCollection(of T)类的新实例。
///
公共可观测集合()
:base(){}
///
///初始化System.Collections.ObjectModel.ObservableCollection(of T)类的新实例,该类包含从指定集合复制的元素。
///
///集合:从中复制元素的集合。
///集合参数不能为null。
公共可观测集合(IEnumerable集合)
:base(collection){}
}
}
摘自它不会以您编写的方式工作,原因是您订阅了mainCollection对象的事件,然后将其替换为另一个对象,只引用了相同的变量名。您不需要分配集合,但需要将更新集合的所有元素添加到主集合 编辑: 可观察收集的添加范围:
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) { }
}
}
使用System.Collections.Specialized;
使用System.Collections.Generic;
命名空间System.Collections.ObjectModel
{
///
///表示动态数据集合,该集合在添加、删除项或刷新整个列表时提供通知。
///
///
公共类ObservableCollection:System.Collections.ObjectModel.ObservableCollection
{
///
///将指定集合的元素添加到ObservableCollection(of T)的末尾。
///
公共void AddRange(IEnumerable集合)
{
foreach(集合中的var i)项。添加(i);
OnCollectionChanged(新建NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add)、collection.ToList());
}
///
///从ObservableCollection(of T)中删除指定集合中每个项的第一次出现。
///
公共空间