Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DerivedObservableCollection<TBase, TValue> : ICollection<TValue>, System.Collections.Specialized.INotifyCollectionChanged
- {
- private ICollection<TBase> baseCollection;
- private Func<TBase, TValue> selector;
- public event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;
- public DerivedObservableCollection(ICollection<TBase> baseCollection, Func<TBase, TValue> selector)
- {
- this.baseCollection = baseCollection;
- this.selector = selector;
- var notifyCollectionChanged = (System.Collections.Specialized.INotifyCollectionChanged)this.baseCollection;
- notifyCollectionChanged.CollectionChanged += baseCollection_CollectionChanged;
- }
- void baseCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- OnCollectionChanged(e);
- }
- protected void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- // Convert 'e' so it represents TValue instead of TBase here.
- if (CollectionChanged != null)
- CollectionChanged(this, e);
- }
- #region ICollection<TValue> Members
- public void Add(TValue item)
- {
- throw new NotSupportedException();
- }
- public void Clear()
- {
- throw new NotSupportedException();
- }
- public bool Contains(TValue item)
- {
- return this.baseCollection.Any(i => this.selector(i).Equals(item));
- }
- public void CopyTo(TValue[] array, int arrayIndex)
- {
- throw new NotSupportedException();
- }
- public int Count
- {
- get { return this.baseCollection.Count; }
- }
- public bool IsReadOnly
- {
- get { return true; }
- }
- public bool Remove(TValue item)
- {
- throw new NotSupportedException();
- }
- #endregion
- #region IEnumerable<T> Members
- public IEnumerator<TValue> GetEnumerator()
- {
- return this.baseCollection.Select(i => this.selector(i)).GetEnumerator();
- }
- #endregion
- #region IEnumerable Members
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return this.baseCollection.Select(i => this.selector(i)).GetEnumerator();
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement