Posted By

electroNik on 10/17/10


Tagged

list


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

Tyster
cybermax
shadoh


Observable List


 / Published in: C#
 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6.  
  7. /// <summary>
  8. /// List that fires events when items are changed
  9. /// </summary>
  10. /// <typeparam name="T">Type of list items</typeparam>
  11. public class ObservableList<T> : IList<T>
  12. {
  13. private IList<T> internalList;
  14.  
  15. public class ListChangedEventArgs : EventArgs
  16. {
  17. public int index;
  18. public T item;
  19. public ListChangedEventArgs(int index, T item)
  20. {
  21. this.index = index;
  22. this.item = item;
  23. }
  24. }
  25.  
  26. public delegate void ItemAddedEventHandler(object source, ListChangedEventArgs e);
  27. public delegate void ItemRemovedEventHandler(object source, ListChangedEventArgs e);
  28. public delegate void ListChangedEventHandler(object source, ListChangedEventArgs e);
  29. public delegate void ListClearedEventHandler(object source, EventArgs e);
  30. /// <summary>
  31. /// Fired whenever list item has been changed, added or removed or when list has been cleared
  32. /// </summary>
  33. public event ListChangedEventHandler ListChanged;
  34. /// <summary>
  35. /// Fired when list item has been removed from the list
  36. /// </summary>
  37. public event ItemRemovedEventHandler ItemRemoved;
  38. /// <summary>
  39. /// Fired when item has been added to the list
  40. /// </summary>
  41. public event ItemAddedEventHandler ItemAdded;
  42. /// <summary>
  43. /// Fired when list is cleared
  44. /// </summary>
  45. public event ListClearedEventHandler ListCleared;
  46.  
  47. public ObservableList()
  48. {
  49. internalList = new List<T>();
  50. }
  51.  
  52. public ObservableList(IList<T> list)
  53. {
  54. internalList = list;
  55. }
  56.  
  57. public ObservableList(IEnumerable<T> collection)
  58. {
  59. internalList = new List<T>(collection);
  60. }
  61.  
  62. protected virtual void OnItemAdded(ListChangedEventArgs e)
  63. {
  64. if (ItemAdded != null)
  65. ItemAdded(this, e);
  66. }
  67.  
  68. protected virtual void OnItemRemoved(ListChangedEventArgs e)
  69. {
  70. if (ItemRemoved != null)
  71. ItemRemoved(this, e);
  72. }
  73.  
  74. protected virtual void OnListChanged(ListChangedEventArgs e)
  75. {
  76. if (ListChanged != null)
  77. ListChanged(this, e);
  78. }
  79.  
  80. protected virtual void OnListCleared(EventArgs e)
  81. {
  82. if (ListCleared != null)
  83. ListCleared(this, e);
  84. }
  85.  
  86. public int IndexOf(T item)
  87. {
  88. return internalList.IndexOf(item);
  89. }
  90.  
  91. public void Insert(int index, T item)
  92. {
  93. internalList.Insert(index, item);
  94. OnListChanged(new ListChangedEventArgs(index, item));
  95. }
  96.  
  97. public void RemoveAt(int index)
  98. {
  99. T item = internalList[index];
  100. internalList.Remove(item);
  101. OnListChanged(new ListChangedEventArgs(index, item));
  102. OnItemRemoved(new ListChangedEventArgs(index, item));
  103. }
  104.  
  105. public T this[int index]
  106. {
  107. get { return internalList[index]; }
  108. set
  109. {
  110. internalList[index] = value;
  111. OnListChanged(new ListChangedEventArgs(index, value));
  112. }
  113. }
  114.  
  115. public void Add(T item)
  116. {
  117. internalList.Add(item);
  118. OnListChanged(new ListChangedEventArgs(internalList.IndexOf(item), item));
  119. OnItemAdded(new ListChangedEventArgs(internalList.IndexOf(item), item));
  120. }
  121.  
  122. public void Clear()
  123. {
  124. internalList.Clear();
  125. OnListCleared(new EventArgs());
  126. }
  127.  
  128. public bool Contains(T item)
  129. {
  130. return internalList.Contains(item);
  131. }
  132.  
  133. public void CopyTo(T[] array, int arrayIndex)
  134. {
  135. CopyTo(array, arrayIndex);
  136. }
  137.  
  138. public int Count
  139. {
  140. get { return internalList.Count; }
  141. }
  142.  
  143. public bool IsReadOnly
  144. {
  145. get { return IsReadOnly; }
  146. }
  147.  
  148. public bool Remove(T item)
  149. {
  150. lock (this)
  151. {
  152. int index = internalList.IndexOf(item);
  153. if (internalList.Remove(item))
  154. {
  155. OnListChanged(new ListChangedEventArgs(index, item));
  156. OnItemRemoved(new ListChangedEventArgs(index, item));
  157. return true;
  158. }
  159. else
  160. return false;
  161. }
  162. }
  163.  
  164. public IEnumerator<T> GetEnumerator()
  165. {
  166. return internalList.GetEnumerator();
  167. }
  168.  
  169. IEnumerator IEnumerable.GetEnumerator()
  170. {
  171. return ((IEnumerable)internalList).GetEnumerator();
  172. }
  173. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jimmyw404 on April 30, 2011

Note: I had to change the last method to the following to get this to compile. (It was using the 'generic' version of IEnumerator/IEnumarable).

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return ((System.Collections.IEnumerable)internalList).GetEnumerator();
    }

You need to login to post a comment.