Last active
October 15, 2015 11:49
-
-
Save Snegovikufa/72500f60d3a5a5254603 to your computer and use it in GitHub Desktop.
Wraps List of items to WPF INotifyCollectionChanged interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Linq; | |
using SharpCommon.Extensions; | |
namespace SharpCommon.Helpers | |
{ | |
public class NotifyListWrapper<T> : INotifyCollectionChanged, IList<T> | |
{ | |
private readonly IList<T> _list; | |
public NotifyListWrapper(IList<T> range) | |
{ | |
_list = range; | |
} | |
public T this[int index] | |
{ | |
get | |
{ | |
return _list[index]; | |
} | |
set | |
{ | |
var old = _list[index]; | |
_list[index] = value; | |
OnCollectionChanged(NotifyCollectionChangedAction.Replace, old, value, index); | |
} | |
} | |
public int Count | |
{ | |
get | |
{ | |
return _list.Count; | |
} | |
} | |
public bool IsReadOnly | |
{ | |
get | |
{ | |
return _list.IsReadOnly; | |
} | |
} | |
public void Add(T item) | |
{ | |
_list.Add(item); | |
OnCollectionChanged(NotifyCollectionChangedAction.Add, item); | |
} | |
public void Clear() | |
{ | |
_list.Clear(); | |
OnCollectionReset(); | |
} | |
public bool Contains(T item) | |
{ | |
return _list.Contains(item); | |
} | |
public void CopyTo(T[] array, int arrayIndex) | |
{ | |
_list.CopyTo(array, arrayIndex); | |
} | |
public bool Remove(T item) | |
{ | |
var result = _list.Remove(item); | |
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item); | |
return result; | |
} | |
public int IndexOf(T item) | |
{ | |
return _list.IndexOf(item); | |
} | |
public void Insert(int index, T newItem) | |
{ | |
_list.Insert(index, newItem); | |
var handler = CollectionChanged; | |
if (handler != null) | |
{ | |
OnCollectionChanged(NotifyCollectionChangedAction.Add, newItem, index); | |
} | |
} | |
public void RemoveAt(int index) | |
{ | |
var item = _list[index]; | |
_list.RemoveAt(index); | |
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item); | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return _list.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public event NotifyCollectionChangedEventHandler CollectionChanged; | |
private void OnCollectionReset() | |
{ | |
var handler = CollectionChanged; | |
if (handler != null) | |
{ | |
handler(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); | |
} | |
} | |
private void OnCollectionChanged(NotifyCollectionChangedAction action, object changedItems) | |
{ | |
var handler = CollectionChanged; | |
if (handler != null) | |
{ | |
handler(this, new NotifyCollectionChangedEventArgs(action, changedItems)); | |
} | |
} | |
private void OnCollectionChanged(NotifyCollectionChangedAction action, object newItem, int index) | |
{ | |
var handler = CollectionChanged; | |
if (handler != null) | |
{ | |
handler(this, new NotifyCollectionChangedEventArgs(action, new[] | |
{ | |
newItem | |
}, index)); | |
} | |
} | |
private void OnCollectionChanged(NotifyCollectionChangedAction action, object oldItem, object newItem, int index) | |
{ | |
var handler = CollectionChanged; | |
if (handler != null) | |
{ | |
handler(this, new NotifyCollectionChangedEventArgs(action, new[] | |
{ | |
newItem | |
}, new[] | |
{ | |
oldItem | |
}, index)); | |
} | |
} | |
private void OnCollectionChanged(NotifyCollectionChangedAction action, IList<T> newItems) | |
{ | |
var handler = CollectionChanged; | |
if (handler != null) | |
{ | |
handler(this, new NotifyCollectionChangedEventArgs(action, newItems)); | |
} | |
} | |
public void AddRange(IEnumerable<T> range) | |
{ | |
var newItems = range as IList<T> ?? range.ToList(); | |
_list.AddRange(newItems); | |
foreach (T item in newItems) | |
{ | |
OnCollectionChanged(NotifyCollectionChangedAction.Add, item); | |
} | |
} | |
public bool EqualCollectionReference(List<T> collection) | |
{ | |
return ReferenceEquals(_list, collection); | |
} | |
public void ReplaceCollection(IEnumerable<T> collection) | |
{ | |
_list.Clear(); | |
_list.AddRange(collection); | |
OnCollectionChanged(NotifyCollectionChangedAction.Add, collection); | |
} | |
public bool NotifyList(IList<T> data) | |
{ | |
return _list == data; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment