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)
27 using System
.Collections
.Generic
;
28 using System
.Collections
.Specialized
;
29 using System
.ComponentModel
;
30 using System
.Runtime
.CompilerServices
;
32 namespace System
.Collections
.ObjectModel
35 [TypeForwardedFrom (Consts
.WindowsBase_3_0
)]
36 public class ObservableCollection
<T
> : Collection
<T
>, INotifyCollectionChanged
, INotifyPropertyChanged
{
38 private class Reentrant
: IDisposable
{
39 private int count
= 0;
57 get { return count > 0; }
61 private Reentrant reentrant
= new Reentrant ();
63 public ObservableCollection()
67 public ObservableCollection(IEnumerable
<T
> collection
)
69 throw new NotImplementedException ();
72 public ObservableCollection(List
<T
> list
)
77 public virtual event NotifyCollectionChangedEventHandler CollectionChanged
;
78 protected virtual event PropertyChangedEventHandler PropertyChanged
;
80 event PropertyChangedEventHandler INotifyPropertyChanged
.PropertyChanged
{
81 add { this.PropertyChanged += value; }
82 remove { this.PropertyChanged -= value; }
85 protected IDisposable
BlockReentrancy ()
91 protected void CheckReentrancy ()
93 NotifyCollectionChangedEventHandler eh
= CollectionChanged
;
95 // Only have a problem if we have more than one event listener.
96 if (reentrant
.Busy
&& eh
!= null && eh
.GetInvocationList ().Length
> 1)
97 throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
100 protected override void ClearItems ()
106 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Reset
));
107 OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
108 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
111 protected override void InsertItem (int index
, T item
)
115 base.InsertItem (index
, item
);
117 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Add
, item
, index
));
118 OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
119 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
122 public void Move (int oldIndex
, int newIndex
)
124 MoveItem (oldIndex
, newIndex
);
127 protected virtual void MoveItem (int oldIndex
, int newIndex
)
131 T item
= Items
[oldIndex
];
132 base.RemoveItem (oldIndex
);
133 base.InsertItem (newIndex
, item
);
135 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Move
, item
, newIndex
, oldIndex
));
136 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
139 protected virtual void OnCollectionChanged (NotifyCollectionChangedEventArgs e
)
141 NotifyCollectionChangedEventHandler eh
= CollectionChanged
;
144 // Make sure that the invocation is done before the collection changes,
145 // Otherwise there's a chance of data corruption.
146 using (BlockReentrancy ()) {
152 protected virtual void OnPropertyChanged (PropertyChangedEventArgs e
)
154 PropertyChangedEventHandler eh
= PropertyChanged
;
160 protected override void RemoveItem (int index
)
164 T item
= Items
[index
];
166 base.RemoveItem (index
);
168 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Remove
, item
, index
));
169 OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
170 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
173 protected override void SetItem (int index
, T item
)
177 T oldItem
= Items
[index
];
179 base.SetItem (index
, item
);
181 OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction
.Replace
, item
, oldItem
, index
));
182 OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));