1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
public class CsutomSortedList<TKey, TValue> : SortedList<TKey, TValue>, INotifyListSource<TValue>
{
private KeyValuePair<TKey, TValue> GetPair(TKey key)
{
foreach (KeyValuePair<TKey, TValue> pair in this)
if (pair.Key.Equals(key))
return pair;
return new KeyValuePair<TKey,TValue>(key, default(TValue));
}
public new void Add(TKey key, TValue value)
{
base.Add(key, value);
this.OnItemAdded(GetPair(key));
}
public new void Clear()
{
base.Clear();
this.OnCollectionReset();
}
public new bool Remove(TKey key)
{
int index = this.IndexOfKey(key);
bool result = base.Remove(key);
this.OnItemRemoved(GetPair(key), index);
return result;
}
public new void RemoveAt(int index)
{
var item = GetPair(Keys[index]);
base.RemoveAt(index);
this.OnItemRemoved(item, index);
}
#region INotifyCollectionChanged Members
public event NotifyCollectionChangedEventHandler CollectionChanged;
/// <summary>
/// Fires the <see cref="CollectionChanged"/> event to indicate an item has been
/// added to the end of the collection.
/// </summary>
/// <param name="item">Item added to the collection.</param>
protected void OnItemAdded(KeyValuePair<TKey, TValue> item)
{
if (this.CollectionChanged != null)
{
this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Add, item, this.Count - 1));
}
}
/// <summary>
/// Fires the <see cref="CollectionChanged"/> event to indicate the collection
/// has been reset. This is used when the collection has been cleared or
/// entirely replaced.
/// </summary>
protected void OnCollectionReset()
{
if (this.CollectionChanged != null)
{
this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Reset));
}
}
/// <summary>
/// Fires the <see cref="CollectionChanged"/> event to indicate an item has
/// been inserted into the collection at the specified index.
/// </summary>
/// <param name="index">Index the item has been inserted at.</param>
/// <param name="item">Item inserted into the collection.</param>
protected void OnItemInserted(int index, KeyValuePair<TKey, TValue> item)
{
if (this.CollectionChanged != null)
{
this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Add, item, index));
}
}
/// <summary>
/// Fires the <see cref="CollectionChanged"/> event to indicate an item has
/// been removed from the collection at the specified index.
/// </summary>
/// <param name="item">Item removed from the collection.</param>
/// <param name="index">Index the item has been removed from.</param>
protected void OnItemRemoved(KeyValuePair<TKey, TValue> item, int index)
{
if (this.CollectionChanged != null)
{
this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Remove, item, index));
}
}
#endregion
#region IListSource Members
public bool ContainsListCollection
{
get { return false; }
}
public IList<TValue> GetList()
{
return Values;
}
#endregion
} |
Partager