3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Collections.Specialized;
6 using System.ComponentModel;
9 namespace SiliconStudio.Presentation.Collections
11 public class ObservableList<T> : IList<T>, IObservableCollection<T>, IReadOnlyObservableCollection<T>
13 private readonly List<T> list;
22 list =
new List<T>(collection);
25 public T
this[
int index]
33 var oldItem = list[index];
35 var arg =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, value, oldItem, index);
36 OnCollectionChanged(arg);
40 public int Count {
get {
return list.Count; } }
42 public bool IsReadOnly {
get {
return false; } }
55 return list.GetEnumerator();
58 IEnumerator IEnumerable.GetEnumerator()
60 return GetEnumerator();
63 public void Add(T item)
70 var itemList = items.ToList();
71 if (itemList.Count > 0)
73 list.AddRange(itemList);
75 var arg =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, itemList, Count - itemList.Count);
76 OnCollectionChanged(arg);
83 var arg =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
84 OnCollectionChanged(arg);
89 return list.Contains(item);
92 public void CopyTo(T[] array,
int arrayIndex)
94 list.CopyTo(array, arrayIndex);
99 int index = list.IndexOf(item);
109 var oldItems = list.Skip(index).Take(count).ToList();
110 list.RemoveRange(index,
count);
111 var arg =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItems, index);
112 OnCollectionChanged(arg);
117 return list.IndexOf(item);
122 list.Insert(index, item);
123 var arg =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index);
124 OnCollectionChanged(arg);
129 var item = list[index];
130 list.RemoveAt(index);
131 var arg =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index);
132 OnCollectionChanged(arg);
137 var arg =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
138 OnCollectionChanged(arg);
144 return string.Format(
"{{ObservableList}} Count = {0}", Count);
149 var handler = CollectionChanged;
157 case NotifyCollectionChangedAction.Add:
158 case NotifyCollectionChangedAction.Remove:
159 case NotifyCollectionChangedAction.Reset:
167 var handler = PropertyChanged;
void Insert(int index, T item)
void AddRange(IEnumerable< T > items)
void OnPropertyChanged(PropertyChangedEventArgs arg)
override string ToString()
ObservableList(IEnumerable< T > collection)
void RemoveRange(int index, int count)
IEnumerator< T > GetEnumerator()
A class that wraps an instance of the ObservableList{T} class and implement the IList interface...
void CopyTo(T[] array, int arrayIndex)
PropertyChangedEventHandler PropertyChanged
NotifyCollectionChangedEventHandler CollectionChanged
void OnCollectionChanged(NotifyCollectionChangedEventArgs arg)