Qualche extension methods (a mio avviso) molto comodo:
1: public static class ObservableCollectionExtensions
2: {
3: public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> collection)
4: {
5: if (collection == null) return new ObservableCollection<T>();
6: return new ObservableCollection<T>(collection);
7: }
8:
9: public static ObservableCollection<T> AddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> range)
10: {
11: foreach (var s in range)
12: collection.Add(s);
13: return collection;
14: }
15:
16: public static ObservableCollection<T> RemoveRange<T>(this ObservableCollection<T> collection, IEnumerable<T> range)
17: {
18: var tempRange = range.ToList();
19: foreach (var deletableItem in tempRange)
20: {
21: if (collection.Contains(deletableItem))
22: collection.Remove(deletableItem);
23: }
24: return collection;
25: }
26:
27: public static ObservableCollection<T> RemoveRange<T>(this ObservableCollection<T> collection, Func<T, bool> action)
28: {
29: return collection.RemoveRange(collection.Where(action));
30: }
31: }
Riccardo.
posted @ martedì 22 febbraio 2011 18:29