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 190 191 192 193 194 195 196 197 198 199 200
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
// Copyright 2011 Xamarin Inc.
//
// Authors:
// Chris Toshok (toshok@novell.com)
// Brian O'Keefe (zer0keefie@gmail.com)
// Marek Safar (marek.safar@gmail.com)
//
#if NET_4_0
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace System.Collections.ObjectModel
{
[Serializable]
#if !MOBILE
[TypeForwardedFrom (Consts.WindowsBase_3_0)]
#endif
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged {
[Serializable]
#if !MOBILE
[TypeForwardedFrom (Consts.WindowsBase_3_0)]
#endif
sealed class SimpleMonitor : IDisposable {
private int _busyCount;
public SimpleMonitor()
{
}
public void Enter()
{
_busyCount++;
}
public void Dispose()
{
_busyCount--;
}
public bool Busy
{
get { return _busyCount > 0; }
}
}
private SimpleMonitor _monitor = new SimpleMonitor ();
public ObservableCollection ()
{
}
public ObservableCollection (IEnumerable<T> collection)
{
if (collection == null)
throw new ArgumentNullException ("collection");
foreach (var item in collection)
Add (item);
}
public ObservableCollection (List<T> list)
: base (list != null ? new List<T> (list) : null)
{
}
[field:NonSerialized]
public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
[field:NonSerialized]
protected virtual event PropertyChangedEventHandler PropertyChanged;
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged {
add { this.PropertyChanged += value; }
remove { this.PropertyChanged -= value; }
}
protected IDisposable BlockReentrancy ()
{
_monitor.Enter ();
return _monitor;
}
protected void CheckReentrancy ()
{
NotifyCollectionChangedEventHandler eh = CollectionChanged;
// Only have a problem if we have more than one event listener.
if (_monitor.Busy && eh != null && eh.GetInvocationList ().Length > 1)
throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
}
protected override void ClearItems ()
{
CheckReentrancy ();
base.ClearItems ();
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
}
protected override void InsertItem (int index, T item)
{
CheckReentrancy ();
base.InsertItem (index, item);
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index));
OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
}
public void Move (int oldIndex, int newIndex)
{
MoveItem (oldIndex, newIndex);
}
protected virtual void MoveItem (int oldIndex, int newIndex)
{
CheckReentrancy ();
T item = Items [oldIndex];
base.RemoveItem (oldIndex);
base.InsertItem (newIndex, item);
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Move, item, newIndex, oldIndex));
OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
}
protected virtual void OnCollectionChanged (NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler eh = CollectionChanged;
if (eh != null) {
// Make sure that the invocation is done before the collection changes,
// Otherwise there's a chance of data corruption.
using (BlockReentrancy ()) {
eh (this, e);
}
}
}
protected virtual void OnPropertyChanged (PropertyChangedEventArgs e)
{
PropertyChangedEventHandler eh = PropertyChanged;
if (eh != null)
eh (this, e);
}
protected override void RemoveItem (int index)
{
CheckReentrancy ();
T item = Items [index];
base.RemoveItem (index);
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, item, index));
OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
}
protected override void SetItem (int index, T item)
{
CheckReentrancy ();
T oldItem = Items [index];
base.SetItem (index, item);
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Replace, item, oldItem, index));
OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
}
}
}
#endif
|