扩展ObservableCollection<T>,增加AddRange<T>()方法

扩展ObservableCollection,实现添加集合的扩展,当集合中新增、修改或者删除项目时,或者集合被刷新时,触发通知(通过实现接口INotifyCollectionChanged)。

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ReflectionIT.WPF {

public static class Extensions {

public static void AddRange<T>(this ObservableCollection<T> oc, IEnumerable<T> collection) {
if (collection == null) {
throw new ArgumentNullException("collection");
}
foreach (var item in collection)
{
oc.Add(item);
}


}
}
}

示例:

1
list.AddRange(anotherList);
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×