1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Collections.Specialized;
|
---|
6 | using System.Collections.ObjectModel;
|
---|
7 |
|
---|
8 | namespace Microsoft.Research.DynamicDataDisplay.Common
|
---|
9 | {
|
---|
10 | public class ObservableCollectionWrapper<T> : INotifyCollectionChanged, IList<T>
|
---|
11 | {
|
---|
12 | public ObservableCollectionWrapper() : this(new ObservableCollection<T>()) { }
|
---|
13 |
|
---|
14 | private readonly ObservableCollection<T> collection;
|
---|
15 | public ObservableCollectionWrapper(ObservableCollection<T> collection)
|
---|
16 | {
|
---|
17 | if (collection == null)
|
---|
18 | throw new ArgumentNullException("collection");
|
---|
19 |
|
---|
20 | this.collection = collection;
|
---|
21 | collection.CollectionChanged += new NotifyCollectionChangedEventHandler(collection_CollectionChanged);
|
---|
22 | }
|
---|
23 |
|
---|
24 | private int attemptsToRaiseChanged = 0;
|
---|
25 | private bool raiseEvents = true;
|
---|
26 |
|
---|
27 | public bool RaisingEvents
|
---|
28 | {
|
---|
29 | get { return raiseEvents; }
|
---|
30 | }
|
---|
31 |
|
---|
32 | private void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
---|
33 | {
|
---|
34 | attemptsToRaiseChanged++;
|
---|
35 | if (raiseEvents)
|
---|
36 | {
|
---|
37 | CollectionChanged.Raise(this, e);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | #region Update methods
|
---|
42 |
|
---|
43 | public void BeginUpdate()
|
---|
44 | {
|
---|
45 | attemptsToRaiseChanged = 0;
|
---|
46 | raiseEvents = false;
|
---|
47 | }
|
---|
48 | public void EndUpdate()
|
---|
49 | {
|
---|
50 | raiseEvents = true;
|
---|
51 | if (attemptsToRaiseChanged > 0)
|
---|
52 | CollectionChanged.Raise(this);
|
---|
53 | }
|
---|
54 |
|
---|
55 | public IDisposable BlockEvents()
|
---|
56 | {
|
---|
57 | return new EventBlocker<T>(this);
|
---|
58 | }
|
---|
59 |
|
---|
60 | private sealed class EventBlocker<TT> : IDisposable
|
---|
61 | {
|
---|
62 | private readonly ObservableCollectionWrapper<TT> collection;
|
---|
63 | public EventBlocker(ObservableCollectionWrapper<TT> collection)
|
---|
64 | {
|
---|
65 | this.collection = collection;
|
---|
66 | collection.BeginUpdate();
|
---|
67 | }
|
---|
68 |
|
---|
69 | #region IDisposable Members
|
---|
70 |
|
---|
71 | public void Dispose()
|
---|
72 | {
|
---|
73 | collection.EndUpdate();
|
---|
74 | }
|
---|
75 |
|
---|
76 | #endregion
|
---|
77 | }
|
---|
78 |
|
---|
79 | #endregion // end of Update methods
|
---|
80 |
|
---|
81 | #region IList<T> Members
|
---|
82 |
|
---|
83 | public int IndexOf(T item)
|
---|
84 | {
|
---|
85 | return collection.IndexOf(item);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public void Insert(int index, T item)
|
---|
89 | {
|
---|
90 | collection.Insert(index, item);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public void RemoveAt(int index)
|
---|
94 | {
|
---|
95 | collection.RemoveAt(index);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public T this[int index]
|
---|
99 | {
|
---|
100 | get
|
---|
101 | {
|
---|
102 | return collection[index];
|
---|
103 | }
|
---|
104 | set
|
---|
105 | {
|
---|
106 | collection[index] = value;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region ICollection<T> Members
|
---|
113 |
|
---|
114 | public void Add(T item)
|
---|
115 | {
|
---|
116 | collection.Add(item);
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void Clear()
|
---|
120 | {
|
---|
121 | collection.Clear();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public bool Contains(T item)
|
---|
125 | {
|
---|
126 | return collection.Contains(item);
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void CopyTo(T[] array, int arrayIndex)
|
---|
130 | {
|
---|
131 | collection.CopyTo(array, arrayIndex);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public int Count
|
---|
135 | {
|
---|
136 | get { return collection.Count; }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public bool IsReadOnly
|
---|
140 | {
|
---|
141 | get { throw new NotImplementedException(); }
|
---|
142 | }
|
---|
143 |
|
---|
144 | public bool Remove(T item)
|
---|
145 | {
|
---|
146 | return collection.Remove(item);
|
---|
147 | }
|
---|
148 |
|
---|
149 | #endregion
|
---|
150 |
|
---|
151 | #region IEnumerable<T> Members
|
---|
152 |
|
---|
153 | public IEnumerator<T> GetEnumerator()
|
---|
154 | {
|
---|
155 | return collection.GetEnumerator();
|
---|
156 | }
|
---|
157 |
|
---|
158 | #endregion
|
---|
159 |
|
---|
160 | #region IEnumerable Members
|
---|
161 |
|
---|
162 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
---|
163 | {
|
---|
164 | return GetEnumerator();
|
---|
165 | }
|
---|
166 |
|
---|
167 | #endregion
|
---|
168 |
|
---|
169 | #region INotifyCollectionChanged Members
|
---|
170 |
|
---|
171 | public event NotifyCollectionChangedEventHandler CollectionChanged;
|
---|
172 |
|
---|
173 | #endregion
|
---|
174 | }
|
---|
175 | }
|
---|