64 lines
2.2 KiB
C#
64 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Threading;
|
|
|
|
namespace Torch.Collections
|
|
{
|
|
[Serializable]
|
|
public class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged, INotifyPropertyChanged
|
|
{
|
|
/// <inheritdoc />
|
|
public new void Add(TKey key, TValue value)
|
|
{
|
|
base.Add(key, value);
|
|
var kv = new KeyValuePair<TKey, TValue>(key, value);
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, kv));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public new bool Remove(TKey key)
|
|
{
|
|
if (!ContainsKey(key))
|
|
return false;
|
|
|
|
var kv = new KeyValuePair<TKey, TValue>(key, this[key]);
|
|
base.Remove(key);
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, kv));
|
|
return true;
|
|
}
|
|
|
|
private void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
|
|
{
|
|
NotifyCollectionChangedEventHandler collectionChanged = CollectionChanged;
|
|
if (collectionChanged != null)
|
|
foreach (NotifyCollectionChangedEventHandler nh in collectionChanged.GetInvocationList())
|
|
{
|
|
var dispObj = nh.Target as DispatcherObject;
|
|
|
|
var dispatcher = dispObj?.Dispatcher;
|
|
if (dispatcher != null && !dispatcher.CheckAccess())
|
|
{
|
|
dispatcher.BeginInvoke(
|
|
(Action)(() => nh.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))),
|
|
DispatcherPriority.DataBind);
|
|
continue;
|
|
}
|
|
|
|
nh.Invoke(this, e);
|
|
}
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
|
|
|
/// <inheritdoc />
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
}
|
|
}
|