Skip to content

Instantly share code, notes, and snippets.

@fabiomarreco
Created November 22, 2017 18:26
Show Gist options
  • Save fabiomarreco/6b9de47ab3629d830dc3ea91d5405399 to your computer and use it in GitHub Desktop.
Save fabiomarreco/6b9de47ab3629d830dc3ea91d5405399 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RiskControl.Common.Tools.Types
{
public class ObservableDictionary<TKey, TVal> : IDictionary<TKey, TVal>, INotifyCollectionChanged, INotifyPropertyChanged
{
private readonly IDictionary<TKey, TVal> _dict;
public ObservableDictionary()
{
_dict = new Dictionary<TKey, TVal>();
}
public ObservableDictionary(IDictionary<TKey, TVal> dictionary)
{
_dict = new Dictionary<TKey, TVal>(dictionary);
}
public IEnumerator<KeyValuePair<TKey, TVal>> GetEnumerator() => _dict.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
public bool Contains(KeyValuePair<TKey, TVal> item) => _dict.Contains(item);
public int Count => _dict.Count;
public bool IsReadOnly => _dict.IsReadOnly;
public bool ContainsKey(TKey key) => _dict.ContainsKey(key);
public void CopyTo(KeyValuePair<TKey, TVal>[] array, int arrayIndex) => _dict.CopyTo(array, arrayIndex);
public bool TryGetValue(TKey key, out TVal value) => _dict.TryGetValue(key, out value);
public ICollection<TKey> Keys => _dict.Keys;
public ICollection<TVal> Values => _dict.Values;
public TVal this[TKey key]
{
get { return _dict[key]; }
set
{
TVal oldItem;
if (_dict.TryGetValue(key, out oldItem))
{
if (object.Equals(oldItem, value))
return;
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, new KeyValuePair<TKey, TVal>(key, oldItem), new KeyValuePair<TKey, TVal>(key, value)));
}
else
{
_dict[key] = value;
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new KeyValuePair<TKey, TVal>(key, value)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
}
}
}
public void Add(KeyValuePair<TKey, TVal> item)
{
_dict.Add(item);
CollectionChanged?.Invoke(this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
}
public void Clear()
{
if (!_dict.Any())
return;
_dict.Clear();
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
}
public bool Remove(KeyValuePair<TKey, TVal> item)
{
if (!_dict.Remove(item))
return false;
CollectionChanged?.Invoke(this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
return true;
}
public void Add(TKey key, TVal value)
{
_dict.Add(key, value);
CollectionChanged?.Invoke(this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new KeyValuePair<TKey, TVal>(key, value)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
}
public bool Remove(TKey key)
{
TVal oldItem;
if (!_dict.TryGetValue(key, out oldItem))
return false;
if (!_dict.Remove(key))
return false;
CollectionChanged?.Invoke(this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, new KeyValuePair<TKey, TVal>(key, oldItem)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Count)));
return true;
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment