ObservableCollection.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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(List<T> list)
  55. : base (list)
  56. {
  57. }
  58. public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
  59. protected virtual event PropertyChangedEventHandler PropertyChanged;
  60. event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged {
  61. add { this.PropertyChanged += value; }
  62. remove { this.PropertyChanged -= value; }
  63. }
  64. protected IDisposable BlockReentrancy ()
  65. {
  66. reentrant.Enter ();
  67. return reentrant;
  68. }
  69. protected void CheckReentrancy ()
  70. {
  71. NotifyCollectionChangedEventHandler eh = CollectionChanged;
  72. // Only have a problem if we have more than one event listener.
  73. if (reentrant.Busy && eh != null && eh.GetInvocationList ().Length > 1)
  74. throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
  75. }
  76. protected override void ClearItems ()
  77. {
  78. CheckReentrancy ();
  79. base.ClearItems ();
  80. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
  81. OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
  82. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  83. }
  84. protected override void InsertItem (int index, T item)
  85. {
  86. CheckReentrancy ();
  87. base.InsertItem (index, item);
  88. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index));
  89. OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
  90. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  91. }
  92. public void Move (int oldIndex, int newIndex)
  93. {
  94. MoveItem (oldIndex, newIndex);
  95. }
  96. protected virtual void MoveItem (int oldIndex, int newIndex)
  97. {
  98. CheckReentrancy ();
  99. T item = Items [oldIndex];
  100. base.RemoveItem (oldIndex);
  101. base.InsertItem (newIndex, item);
  102. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Move, item, newIndex, oldIndex));
  103. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  104. }
  105. protected virtual void OnCollectionChanged (NotifyCollectionChangedEventArgs e)
  106. {
  107. NotifyCollectionChangedEventHandler eh = CollectionChanged;
  108. if (eh != null) {
  109. // Make sure that the invocation is done before the collection changes,
  110. // Otherwise there's a chance of data corruption.
  111. using (BlockReentrancy ()) {
  112. eh (this, e);
  113. }
  114. }
  115. }
  116. protected virtual void OnPropertyChanged (PropertyChangedEventArgs e)
  117. {
  118. PropertyChangedEventHandler eh = PropertyChanged;
  119. if (eh != null)
  120. eh (this, e);
  121. }
  122. protected override void RemoveItem (int index)
  123. {
  124. CheckReentrancy ();
  125. T item = Items [index];
  126. base.RemoveItem (index);
  127. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, item, index));
  128. OnPropertyChanged (new PropertyChangedEventArgs ("Count"));
  129. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  130. }
  131. protected override void SetItem (int index, T item)
  132. {
  133. CheckReentrancy ();
  134. T oldItem = Items [index];
  135. base.SetItem (index, item);
  136. OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Replace, item, oldItem, index));
  137. OnPropertyChanged (new PropertyChangedEventArgs ("Item[]"));
  138. }
  139. }
  140. }