INotifyCollectionChanged 用法简介
INotifyCollectionChanged 接口是 System.Collections.Specialized 命名空间中的一个接口,用于在集合发生更改时通知订阅者。这个接口通常在实现了集合的类中使用,以便在集合变化时通知监听者(如 UI 控件)进行更新。
以下是 INotifyCollectionChanged 的关键成员:
CollectionChanged 事件: 当集合发生变化时触发的事件。这个事件包含一个 NotifyCollectionChangedEventArgs 参数,该参数提供了有关集合变化的详细信息,如变化的类型(添加、移除、替换等)、受影响的索引和元素。
NotifyCollectionChangedEventArgs 类: 包含有关集合变化的详细信息的类。常见的属性包括:
Action:表示变化的类型,是一个 NotifyCollectionChangedAction 枚举值,包括 Add、Remove、Replace、Move 和 Reset。
NewItems:在 Add、Replace 和 Move 操作中,表示添加或替换的新元素的集合。
OldItems:在 Remove、Replace 和 Move 操作中,表示移除或替换的旧元素的集合。
NewStartingIndex 和 OldStartingIndex:在 Move 操作中,表示移动操作的起始索引。
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
public class ExampleCollection : ObservableCollection<string>
{
public ExampleCollection()
{
this.CollectionChanged += OnCollectionChanged;
}
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Console.WriteLine($"Collection changed: {e.Action}");
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
Console.WriteLine($"Added item(s): {string.Join(", ", e.NewItems)}");
break;
case NotifyCollectionChangedAction.Remove:
Console.WriteLine($"Removed item(s): {string.Join(", ", e.OldItems)}");
break;
case NotifyCollectionChangedAction.Replace:
Console.WriteLine($"Replaced item(s): {string.Join(", ", e.NewItems)}");
break;
case NotifyCollectionChangedAction.Move://
Console.WriteLine($"Moved item(s) from index {e.OldStartingIndex} to {e.NewStartingIndex}");
break;
case NotifyCollectionChangedAction.Reset://整个集合被重置,即调用Clear方法,而不是重新赋值为另一个对象!例如a = new ObservableCollection<int>() { 3, 4, 5 };
Console.WriteLine("Collection was reset");
break;
}
}
}
class Program
{
static void Main()
{
ExampleCollection collection = new ExampleCollection();
collection.Add("Item 1");
collection.Add("Item 2");
collection.Remove("Item 1");
collection[0] = "New Item 1";
}
}
static void Main(){
var a = new ObservableCollection<int>();
a.CollectionChanged += A_CollectionChanged;
a.Add(1);
a.RemoveAt(0);
a.Add(3);
a.Add(5);
//a = new ObservableCollection<int>() { 3, 4, 5 };//不会触发Reset事件
a.Clear();
a.Add(3);
a.Add(4);
a.Move(1, 0);
}
private static void A_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Console.WriteLine($"Collection changed: {e.Action}");
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
Console.WriteLine($"Added item(s): {string.Join(", ", e.NewItems)}");
break;
case NotifyCollectionChangedAction.Remove:
Console.WriteLine($"Removed item(s): {string.Join(", ", e.OldItems)}");
break;
case NotifyCollectionChangedAction.Replace:
Console.WriteLine($"Replaced item(s): {string.Join(", ", e.NewItems)}");
break;
case NotifyCollectionChangedAction.Move://
Console.WriteLine($"Moved item(s) from index {e.OldStartingIndex} to {e.NewStartingIndex}");
break;
case NotifyCollectionChangedAction.Reset://整个集合被重置
Console.WriteLine("Collection was reset");
break;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix