Skip to content

Instantly share code, notes, and snippets.

@fantasticswallow
Created March 11, 2014 12:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fantasticswallow/9484563 to your computer and use it in GitHub Desktop.
Save fantasticswallow/9484563 to your computer and use it in GitHub Desktop.
custom observable collection
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
namespace BindingSampleApp
{
public class ExtendObservableCollection<T> : IList, IList<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
private List<T> source = new List<T>();
private object _syncRoot = new object();
private void notifyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
private void collectionNotifyReset()
{
if (CollectionChanged != null)
{
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
private void collectionNotify(NotifyCollectionChangedAction act,object item)
{
if (CollectionChanged != null)
{
CollectionChanged(this, new NotifyCollectionChangedEventArgs(act,item));
}
}
private void collectionNotify(NotifyCollectionChangedAction act, object item,int index)
{
if (CollectionChanged != null)
{
CollectionChanged(this, new NotifyCollectionChangedEventArgs(act, item, index));
}
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
public int IndexOf(T item)
{
return source.IndexOf(item);
}
public void Insert(int index, T item)
{
source.Insert(index, item);
notifyChanged("Count");
notifyChanged("Item[]");
collectionNotify(NotifyCollectionChangedAction.Add, item, index);
}
public void RemoveAt(int index)
{
if (index >= 0)
{
var item = source[index];
source.RemoveAt(index);
notifyChanged("Count");
notifyChanged("Item[]");
collectionNotify(NotifyCollectionChangedAction.Remove, item, index);
}
}
public T this[int index]
{
get
{
return source[index];
}
set
{
source[index] = value;
notifyChanged("Item[]");
collectionNotify(NotifyCollectionChangedAction.Replace, value, index);
}
}
public void Add(T item)
{
source.Add(item);
notifyChanged("Count");
notifyChanged("Item[]");
collectionNotify(NotifyCollectionChangedAction.Add, item, source.Count - 1);
}
public void Clear()
{
source.Clear();
notifyChanged("Count");
notifyChanged("Item[]");
collectionNotifyReset();
}
public bool Contains(T item)
{
return source.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
source.CopyTo(array, arrayIndex);
}
public int Count
{
get { return source.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
if (source.Contains(item))
{
var idx = source.IndexOf(item);
var res = source.Remove(item);
if (res)
{
notifyChanged("Count");
notifyChanged("Item[]");
collectionNotify(NotifyCollectionChangedAction.Remove, item,idx);
}
return res;
}
else
{
return false;
}
}
public IEnumerator<T> GetEnumerator()
{
return source.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return source.GetEnumerator();
}
public int Add(object value)
{
try
{
source.Add((T)value);
notifyChanged("Count");
notifyChanged("Item[]");
collectionNotify(NotifyCollectionChangedAction.Add, value, source.Count - 1);
return source.Count - 1;
}
catch
{
return -1;
}
}
public bool Contains(object value)
{
try
{
return source.Contains((T)value);
}
catch
{
return false;
}
}
public int IndexOf(object value)
{
try
{
return source.IndexOf((T)value);
}
catch
{
return -1;
}
}
public void Insert(int index, object value)
{
source.Insert(index, (T)value);
notifyChanged("Count");
notifyChanged("Item[]");
collectionNotify(NotifyCollectionChangedAction.Add, value, index);
}
public bool IsFixedSize
{
get { return false; }
}
public void Remove(object value)
{
source.Remove((T)value);
notifyChanged("Count");
notifyChanged("Item[]");
}
object IList.this[int index]
{
get
{
return source[index];
}
set
{
source[index] = (T)value;
notifyChanged("Item[]");
collectionNotify(NotifyCollectionChangedAction.Replace, value, index);
}
}
public void CopyTo(Array array, int index)
{
source.CopyTo((T[])array, index);
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get { return _syncRoot; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment