ObservableCollection.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Chris Toshok ([email protected])
  24. // Brian O'Keefe ([email protected])
  25. //
  26. using System.Collections.Generic;
  27. using System.Collections.Specialized;
  28. using System.ComponentModel;
  29. namespace System.Collections.ObjectModel {
  30. [Serializable]
  31. public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged {
  32. private class Reentrant : IDisposable {
  33. private int count = 0;
  34. public Reentrant()
  35. {
  36. }
  37. public void Enter()
  38. {
  39. count++;
  40. }
  41. public void Dispose()
  42. {
  43. count--;
  44. }
  45. public bool Busy
  46. {
  47. get { return count > 0; }
  48. }
  49. }
  50. private Reentrant reentrant = new Reentrant ();
  51. public ObservableCollection()
  52. {
  53. }
  54. public ObservableCollection(IEnumerable<T> collection)
  55. {
  56. throw new NotImplementedException ();
  57. }
  58. public ObservableCollection(List<T> list)
  59. : base (list)
  60. {
  61. }
  62. public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
  63. protected virtual event PropertyChangedEventHandler PropertyChanged;
  64. event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged {
  65. add { this.PropertyChanged += value; }
  66. remove { this.PropertyChanged -= value; }
  67. }
  68. protected IDisposable BlockReentrancy ()
  69. {
  70. reentrant.Enter ();
  71. return reentrant;
  72. }
  73. protected void CheckReentrancy ()
  74. {
  75. NotifyCollectionChangedEventHandler eh = CollectionChanged;
  76. // Only have a problem if we have more than one event listener.
  77. if (reentrant.Busy && eh != null && eh.GetInvocationList ().Length > 1)
  78. throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
  79. }
  80. protected override void ClearItems ()
  81. {
  82. CheckReentrancy ();
  83. base.ClearItems ();
  84. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
  85. OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
  86. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  87. }
  88. protected override void InsertItem (int index, T item)
  89. {
  90. CheckReentrancy ();
  91. base.InsertItem (index, item);
  92. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index));
  93. OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
  94. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  95. }
  96. public void Move (int oldIndex, int newIndex)
  97. {
  98. MoveItem (oldIndex, newIndex);
  99. }
  100. protected virtual void MoveItem (int oldIndex, int newIndex)
  101. {
  102. CheckReentrancy ();
  103. T item = Items [oldIndex];
  104. base.RemoveItem (oldIndex);
  105. base.InsertItem (newIndex, item);
  106. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Move, item, newIndex, oldIndex));
  107. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  108. }
  109. protected virtual void OnCollectionChanged (NotifyCollectionChangedEventArgs e)
  110. {
  111. NotifyCollectionChangedEventHandler eh = CollectionChanged;
  112. if (eh != null) {
  113. // Make sure that the invocation is done before the collection changes,
  114. // Otherwise there's a chance of data corruption.
  115. using (BlockReentrancy ()) {
  116. eh (this, e);
  117. }
  118. }
  119. }
  120. protected virtual void OnPropertyChanged (PropertyChangedEventArgs e)
  121. {
  122. PropertyChangedEventHandler eh = PropertyChanged;
  123. if (eh != null)
  124. eh (this, e);
  125. }
  126. protected override void RemoveItem (int index)
  127. {
  128. CheckReentrancy ();
  129. T item = Items [index];
  130. base.RemoveItem (index);
  131. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, item, index));
  132. OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
  133. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  134. }
  135. protected override void SetItem (int index, T item)
  136. {
  137. CheckReentrancy ();
  138. T oldItem = Items [index];
  139. base.SetItem (index, item);
  140. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Replace, item, oldItem, index));
  141. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  142. }
  143. }
  144. }