9using System.Collections;
10using System.Collections.Generic;
11using System.Collections.Specialized;
13using System.Linq.Expressions;
24 IList, ICollection, INotifyCollectionChanged
29 public event System.Collections.Specialized.NotifyCollectionChangedEventHandler
CollectionChanged;
31 private System.Collections.ObjectModel.ObservableCollection<T> _baseCollection;
32 private List<T> _filteredCollection;
33 private bool _suppressEvents =
false;
41 : this(source, queryResult.ToList())
52 _filteredCollection = queryResult;
53 _baseCollection = source;
54 _baseCollection.CollectionChanged += (o, e) =>
58 NotifyCollectionChangedEventArgs newE =
null;
63 case NotifyCollectionChangedAction.Add:
64 item = (T)e.NewItems[0];
65 index = e.NewStartingIndex;
66 if (index > _filteredCollection.Count)
67 index = _filteredCollection.Count;
68 _filteredCollection.Insert(index, item);
69 newE =
new NotifyCollectionChangedEventArgs(
70 e.Action, item, _filteredCollection.IndexOf(item));
72 case NotifyCollectionChangedAction.Remove:
73 item = (T)e.OldItems[0];
74 index = _filteredCollection.IndexOf(item);
77 _filteredCollection.Remove(item);
78 newE =
new NotifyCollectionChangedEventArgs(e.Action, item, index);
81 case NotifyCollectionChangedAction.Replace:
82 index = _filteredCollection.IndexOf((T)e.OldItems[0]);
85 _filteredCollection[index] = (T)e.NewItems[0];
86 newE =
new NotifyCollectionChangedEventArgs(
88 e.NewItems, e.OldItems, index);
91 case NotifyCollectionChangedAction.Reset:
92 if (source.Count == 0)
94 _filteredCollection.Clear();
95 newE =
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
124 get {
return _baseCollection; }
133 get {
return _filteredCollection; }
142 return _filteredCollection.IndexOf(item);
152 _baseCollection.Insert(index, item);
161 _baseCollection.Remove(_filteredCollection[index]);
169 public T
this[
int index]
173 return _filteredCollection[index];
177 var idx = _baseCollection.IndexOf(_filteredCollection[index]);
178 _baseCollection[idx] = value;
188 _baseCollection.Add(item);
201 _suppressEvents =
true;
202 foreach (var item
in _filteredCollection)
203 _baseCollection.Remove(item);
204 _suppressEvents =
false;
205 _filteredCollection.Clear();
207 new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
217 return _filteredCollection.Contains(item);
225 public void CopyTo(T[] array,
int arrayIndex)
227 _filteredCollection.CopyTo(array, arrayIndex);
235 get {
return _filteredCollection.Count; }
246 var obj = _baseCollection as ICollection<T>;
248 return obj.IsReadOnly;
260 return _baseCollection.Remove(item);
268 return _filteredCollection.GetEnumerator();
274 IEnumerator IEnumerable.GetEnumerator()
276 return _filteredCollection.GetEnumerator();
283 public int Add(
object value)
285 return ((IList)_baseCollection).Add(value);
295 return _filteredCollection.Contains((T)value);
304 return _filteredCollection.IndexOf((T)value);
312 public void Insert(
int index,
object value)
314 _baseCollection.Insert(index, (T)value);
323 get {
return ((IList)_baseCollection).IsFixedSize; }
332 _baseCollection.Remove((T)value);
335 object IList.this[
int index]
343 this[index] = (T)value;
352 public void CopyTo(Array array,
int index)
354 ((IList)_filteredCollection).CopyTo(array, index);
371 get {
return ((IList)_baseCollection).
SyncRoot; }
378 public static class LinqObservableCollectionExtension
384 public static LinqObservableCollection<C> ToSyncList<C>(
this IEnumerable<C> queryResult, System.Collections.ObjectModel.ObservableCollection<C> source)
386 return new LinqObservableCollection<C>(source, queryResult);
393 public static LinqObservableCollection<C> ToSyncList<C>(
this System.Collections.ObjectModel.ObservableCollection<C> source, IEnumerable<C> queryResult)
395 return new LinqObservableCollection<C>(source, queryResult);
402 public static LinqObservableCollection<C> ToSyncList<C>(
this System.Collections.ObjectModel.ObservableCollection<C> source, Expression<Func<C, bool>> expr)
404 IEnumerable<C> sourceEnum = source.AsEnumerable<C>();
405 var output = sourceEnum.Where<C>(expr.Compile());
406 return new LinqObservableCollection<C>(source, output.ToList());
Synchronized view over a source list, filtered, sorted and ordered based on a query result.
System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged
Event raised when the underlying source list is changed.
IList< T > Source
Gets the source list (usually a BusinessListBase) object that contains/manages all data items.
virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
Raises the CollectionChanged event.
bool IsFixedSize
Gets a value indicating whether the source list has a fixed size.
List< T > QueryResult
Gets the query result object used to filter the view.
void Add(T item)
Adds an item to the end of the list.
void Clear()
Clears the list.
void CopyTo(T[] array, int arrayIndex)
Copies the contents of the list to an array.
void RemoveAt(int index)
Removes item at specified index.
void CopyTo(Array array, int index)
Copies the contents of the list to an array.
int Count
Gets the number of items in the list.
void Insert(int index, T item)
Inserts item into specified position in list.
bool IsReadOnly
Gets a value indicating whether the source list is read-only.
int IndexOf(object value)
Gets the positional index of the item.
bool Contains(T item)
Gets a value indicating whether the list contains the specified item.
int Add(object value)
Adds an item to the end of the list.
bool Contains(object value)
Gets a value indicating whether the list contains the specified item.
LinqObservableCollection(System.Collections.ObjectModel.ObservableCollection< T > source, IEnumerable< T > queryResult)
bool Remove(T item)
Removes specified item from the list.
IEnumerator< T > GetEnumerator()
Gets an enumerator for the list.
object SyncRoot
Gets the SyncRoot from the source list.
int IndexOf(T item)
Gets the positional index of the item.
void Remove(object value)
Removes specified item from the list.
void Insert(int index, object value)
Inserts item into specified position in list.
bool IsSynchronized
Gets a value indicating whether the source list is synchronized.
LinqObservableCollection(System.Collections.ObjectModel.ObservableCollection< T > source, List< T > queryResult)
Creates a new instance of the observable view.