-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNotifyingList.cs
189 lines (155 loc) · 4.94 KB
/
NotifyingList.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
namespace Standard
{
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
internal class NotifyingList<T> : IList<T>, INotifyCollectionChanged where T : INotifyPropertyChanged
{
private readonly ObservableCollection<T> _list;
public event PropertyChangedEventHandler ItemPropertyChanged;
private void _SafeAddPropertyListener(T item)
{
if (item != null)
{
item.PropertyChanged += _OnItemPropertyChanged;
}
}
private void _SafeRemovePropertyListener(T item)
{
if (item != null)
{
item.PropertyChanged -= _OnItemPropertyChanged;
}
}
public NotifyingList()
{
_list = new ObservableCollection<T>();
}
public NotifyingList(IEnumerable<T> collection)
{
_list = new ObservableCollection<T>(collection);
foreach (T item in collection)
{
_SafeAddPropertyListener(item);
}
}
private void _OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var handler = ItemPropertyChanged;
if (handler != null)
{
handler(sender, e);
}
}
#region IList<T> Members
public int IndexOf(T item)
{
return _list.IndexOf(item);
}
public void Insert(int index, T item)
{
_list.Insert(index, item);
_SafeAddPropertyListener(item);
}
public void RemoveAt(int index)
{
T item = _list[index];
_list.RemoveAt(index);
_SafeRemovePropertyListener(item);
}
public T this[int index]
{
get { return _list[index]; }
set { _list[index] = value; }
}
#endregion
#region ICollection<T> Members
public void Add(T item)
{
_list.Add(item);
_SafeAddPropertyListener(item);
}
public void Clear()
{
T[] items = new T[_list.Count];
_list.CopyTo(items, 0);
_list.Clear();
foreach (T item in items)
{
_SafeRemovePropertyListener(item);
}
}
public bool Contains(T item)
{
return _list.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_list.CopyTo(array, arrayIndex);
}
public int Count { get { return _list.Count; } }
public bool IsReadOnly { get { return false; } }
public bool Remove(T item)
{
// Don't call through _list.Remove().
// If equality has been overloaded then we may try removing the handler
// from a different object and we'll leak it.
// Ensure reference equality.
int index = _list.IndexOf(item);
if (index == -1)
{
return false;
}
RemoveAt(index);
return true;
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
return _list.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return _list.GetEnumerator();
}
#endregion
#region INotifyCollectionChanged Members
private event NotifyCollectionChangedEventHandler _sourceCollectionChanged;
public event NotifyCollectionChangedEventHandler CollectionChanged
{
add
{
if (_sourceCollectionChanged == null)
{
_list.CollectionChanged += _OnSourceCollectionChanged;
}
_sourceCollectionChanged += value;
}
remove
{
_sourceCollectionChanged -= value;
if (_sourceCollectionChanged == null)
{
_list.CollectionChanged -= _OnSourceCollectionChanged;
}
}
}
private void _OnSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
var handler = _sourceCollectionChanged;
if (handler != null)
{
handler(this, e);
}
}
#endregion
}
}