-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservableList.cs
147 lines (122 loc) · 4.94 KB
/
ObservableList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
namespace Common
{
/// <summary>
/// Found this example of a means to replace ObservableCollection<T> with
/// something that supports adding a range to the collection.
/// See this article: https://baumbartsjourney.wordpress.com/2009/06/01/an-alternative-to-observablecollection/
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IObservableList<T> : IList<T>, INotifyCollectionChanged { }
public class ObservableList<T> : List<T>, IObservableList<T>, INotifyPropertyChanged
{
public ObservableList()
{
IsNotifying = true;
// As a gimmick, I wanted to bind to the Count property, so I
// use the OnPropertyChanged event from the INotifyPropertyChanged
// interface to notify about Count changes.
CollectionChanged += new NotifyCollectionChangedEventHandler(
delegate (object sender, NotifyCollectionChangedEventArgs e)
{
if (sender is null)
{
throw new ArgumentNullException(nameof(sender));
}
if (e is null)
{
throw new ArgumentNullException(nameof(e));
}
NotifyPropertyChanged("Count");
}
);
}
#region Properties
public bool IsNotifying { get; set; }
#endregion
#region Adding and removing items
public new void Add(T item)
{
base.Add(item);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}
public new void AddRange(IEnumerable<T> collection)
{
base.AddRange(collection);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new List<T>(collection)));
}
public new void Clear()
{
base.Clear();
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public new void Insert(int i, T item)
{
base.Insert(i, item);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}
public new void InsertRange(int i, IEnumerable<T> collection)
{
base.InsertRange(i, collection);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection));
}
public new void Remove(T item)
{
base.Remove(item);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
}
public new void RemoveAll(Predicate<T> match)
{
var backup = FindAll(match);
base.RemoveAll(match);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, backup));
}
public new void RemoveAt(int i)
{
var backup = this[i];
base.RemoveAt(i);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, backup));
}
public new void RemoveRange(int index, int count)
{
var backup = GetRange(index, count);
base.RemoveRange(index, count);
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, backup));
}
public new T this[int index]
{
get => base[index];
set
{
T oldValue = base[index];
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, value, oldValue));
}
}
#endregion
#region INotifyCollectionChanged Members
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected void NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (IsNotifying && CollectionChanged != null)
try
{
CollectionChanged(this, e);
}
catch (NotSupportedException)
{
NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}