1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
23 // Chris Toshok (toshok@novell.com)
24 // Brian O'Keefe (zer0keefie@gmail.com)
29 using System
.Collections
.ObjectModel
;
30 using System
.Runtime
.CompilerServices
;
31 [assembly
:TypeForwardedTo (typeof (ObservableCollection
<>))]
35 using System
.Collections
.Generic
;
36 using System
.Collections
.Specialized
;
37 using System
.ComponentModel
;
39 namespace System
.Collections
.ObjectModel
{
41 public class ObservableCollection
<T
> : Collection
<T
>, INotifyCollectionChanged
, INotifyPropertyChanged
{
43 private class Reentrant
: IDisposable
{
44 private int count
= 0;
62 get { return count > 0; }
66 private Reentrant reentrant
= new Reentrant ();
68 public ObservableCollection()
72 public ObservableCollection(IEnumerable
<T
> collection
)
74 throw new NotImplementedException ();
77 public ObservableCollection(List
<T
> list
)
82 public virtual event NotifyCollectionChangedEventHandler CollectionChanged
;
83 protected virtual event PropertyChangedEventHandler PropertyChanged
;
85 event PropertyChangedEventHandler INotifyPropertyChanged
.PropertyChanged
{
86 add { this.PropertyChanged += value; }
87 remove { this.PropertyChanged -= value; }
90 protected IDisposable
BlockReentrancy ()
96 protected void CheckReentrancy ()
98 NotifyCollectionChangedEventHandler eh
= CollectionChanged
;
100 // Only have a problem if we have more than one event listener.
101 if (reentrant
.Busy
&& eh
!= null && eh
.GetInvocationList ().Length
> 1)
102 throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
105 protected override void ClearItems ()
111 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Reset
));
112 OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
113 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
116 protected override void InsertItem (int index
, T item
)
120 base.InsertItem (index
, item
);
122 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Add
, item
, index
));
123 OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
124 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
127 public void Move (int oldIndex
, int newIndex
)
129 MoveItem (oldIndex
, newIndex
);
132 protected virtual void MoveItem (int oldIndex
, int newIndex
)
136 T item
= Items
[oldIndex
];
137 base.RemoveItem (oldIndex
);
138 base.InsertItem (newIndex
, item
);
140 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Move
, item
, newIndex
, oldIndex
));
141 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
144 protected virtual void OnCollectionChanged (NotifyCollectionChangedEventArgs e
)
146 NotifyCollectionChangedEventHandler eh
= CollectionChanged
;
149 // Make sure that the invocation is done before the collection changes,
150 // Otherwise there's a chance of data corruption.
151 using (BlockReentrancy ()) {
157 protected virtual void OnPropertyChanged (PropertyChangedEventArgs e
)
159 PropertyChangedEventHandler eh
= PropertyChanged
;
165 protected override void RemoveItem (int index
)
169 T item
= Items
[index
];
171 base.RemoveItem (index
);
173 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Remove
, item
, index
));
174 OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
175 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
178 protected override void SetItem (int index
, T item
)
182 T oldItem
= Items
[index
];
184 base.SetItem (index
, item
);
186 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Replace
, item
, oldItem
, index
));
187 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));