-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObservableList.cs
182 lines (160 loc) · 4.48 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
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
using System;
using System.Collections.Generic;
namespace Blax;
public interface IObservableList
{
event Action? ListChanged;
}
/// <summary>
/// Represents a list that raises an event when it changes.
/// </summary>
/// <typeparam name="T">The type of the items in the list.</typeparam>
public class ObservableList<T> : List<T>, IObservableList
{
/// <summary>
/// Occurs when the list is changed.
/// </summary>
public event Action? ListChanged;
/// <summary>
/// Adds an object to the end of the List<T>.
/// </summary>
public new void Add(T item)
{
base.Add(item);
ListChanged?.Invoke();
}
/// <summary>
/// Adds the elements of the specified collection to the end of the List<T>.
/// </summary>
public new void AddRange(IEnumerable<T> collection)
{
base.AddRange(collection);
ListChanged?.Invoke();
}
/// <summary>
/// Removes all the elements that match the conditions defined by the specified predicate.
/// </summary>
public new int RemoveAll(Predicate<T> match)
{
var result = base.RemoveAll(match);
if (result > 0) ListChanged?.Invoke();
return result;
}
/// <summary>
/// Removes the first occurrence of a specific object from the List<T>.
/// </summary>
public new bool Remove(T item)
{
var result = base.Remove(item);
if (result) ListChanged?.Invoke();
return result;
}
/// <summary>
/// Removes the all the elements that match the conditions defined by the specified predicate.
/// </summary>
public new void RemoveAt(int index)
{
base.RemoveAt(index);
ListChanged?.Invoke();
}
/// <summary>
/// Removes a range of elements from the List<T>.
/// </summary>
public new void RemoveRange(int index, int count)
{
base.RemoveRange(index, count);
ListChanged?.Invoke();
}
/// <summary>
/// Removes all elements from the List<T>.
/// </summary>
public new void Clear()
{
base.Clear();
ListChanged?.Invoke();
}
/// <summary>
/// Inserts an element into the List<T> at the specified index.
/// </summary>
public new void Insert(int index, T item)
{
base.Insert(index, item);
ListChanged?.Invoke();
}
/// <summary>
/// Inserts the elements of a collection into the List<T> at the specified index.
/// </summary>
public new void InsertRange(int index, IEnumerable<T> collection)
{
base.InsertRange(index, collection);
ListChanged?.Invoke();
}
/// <summary>
/// Sorts the elements in the entire List<T> using the default comparer.
/// </summary>
public new void Sort()
{
base.Sort();
ListChanged?.Invoke();
}
/// <summary>
/// Sorts the elements in the entire List<T> using the specified comparer.
/// </summary>
public new void Sort(IComparer<T> comparer)
{
base.Sort(comparer);
ListChanged?.Invoke();
}
/// <summary>
/// Sorts the elements in the entire List<T> using the specified Comparison<T>.
/// </summary>
public new void Sort(Comparison<T> comparison)
{
base.Sort(comparison);
ListChanged?.Invoke();
}
/// <summary>
/// Sorts the elements in a range of elements in List<T> using the specified comparer.
/// </summary>
public new void Sort(int index, int count, IComparer<T> comparer)
{
base.Sort(index, count, comparer);
ListChanged?.Invoke();
}
/// <summary>
/// Reverses the elements in the entire List<T>.
/// </summary>
public new void Reverse()
{
base.Reverse();
ListChanged?.Invoke();
}
/// <summary>
/// Reverses the elements in a range of elements in List<T>.
/// </summary>
public new void Reverse(int index, int count)
{
base.Reverse(index, count);
ListChanged?.Invoke();
}
/// <summary>
/// Sets or gets the element at the specified index.
/// </summary>
public new T this[int index]
{
get => base[index];
set
{
base[index] = value;
ListChanged?.Invoke();
}
}
/// <summary>
/// Sets the capacity to the actual number of elements in the List<T>, if that number is less than a threshold value.
/// </summary>
public new void TrimExcess()
{
base.TrimExcess();
ListChanged?.Invoke();
}
}