您可以考虑创建一个继承自 ObservalbeCollection 类的自定义 ObservableDictionary < T , U > 类。我已经完成了这项工作,尽管要解决这些问题需要做一些工作,但它已成为我最珍贵的定制课程之一。一些简短的代码作为建议:
/// <summary>Dictionary changed event handler</summary>
/// <param name="sender">The dictionary</param>
/// <param name="e">The event arguments</param>
public delegate void NotifyDictionaryChangedEventHandler(object sender, NotifyDictionaryChangedEventArgs e);
public class CollectionDictionary<TKey, TValue> : ObservableCollection<TValue>
{
#region Fields
private ConcurrentDictionary<TKey, TValue> collectionDictionary =
new ConcurrentDictionary<TKey, TValue>();
#endregion
/// <summary>
/// Initializes a new instance of the CollectionDictionary class
/// </summary>
public CollectionDictionary()
{
}
/// <summary>
/// Initializes a new instance of the CollectionDictionary class
/// </summary>
/// <param name="collectionDictionary">A dictionary</param>
public CollectionDictionary(Dictionary<TKey, TValue> collectionDictionary)
{
for (int i = 0; i < collectionDictionary.Count; i++)
{
this.Add(collectionDictionary.Keys.ToList()[i], collectionDictionary.Values.ToList()[i]);
}
}
/// <summary>
/// Initializes a new instance of the CollectionDictionary class
/// </summary>
/// <param name="collectionDictionary">A concurrent dictionary</param>
public CollectionDictionary(ConcurrentDictionary<TKey, TValue> collectionDictionary)
{
this.collectionDictionary = collectionDictionary;
}
#region Events
/// <summary>The dictionary has changed</summary>
public event NotifyDictionaryChangedEventHandler DictionaryChanged;
#endregion
#region Indexers
/// <summary> Gets the value associated with the specified key. </summary>
/// <param name="key"> The key of the value to get or set. </param>
/// <returns> Returns the Value property of the System.Collections.Generic.KeyValuePair<TKey,TValue>
/// at the specified index. </returns>
public TValue this[TKey key]
{
get
{
TValue tValue;
if (this.collectionDictionary.TryGetValue(key, out tValue) && (key != null))
{
return this.collectionDictionary[key];
}
else
{
return tValue;
}
}
////set
////{
//// this.collectionDictionary[key] = value;
//// string tKey = key.ToString();
//// string tValue = this.collectionDictionary[key].ToString();
//// KeyValuePair<TKey, TValue> genericKeyPair = new KeyValuePair<TKey, TValue>(key, value);
//// List<KeyValuePair<TKey, TValue>> keyList = this.collectionDictionary.ToList();
//// for (int i = 0; i < keyList.Count; i++)
//// {
//// if (genericKeyPair.Key.ToString() == keyList[i].Key.ToString())
//// {
//// RemoveAt(i, String.Empty);
//// Insert(i, value.ToString(), String.Empty);
//// }
//// }
////}
}
/// <summary>
/// Gets the value associated with the specific index
/// </summary>
/// <param name="index">The index</param>
/// <returns>The value at that index</returns>
public new TValue this[int index]
{
get
{
if (index > (this.Count - 1))
{
return default(TValue);
}
else
{
return this.collectionDictionary.ToList()[index].Value;
}
}
}
#endregion
/// <summary>
/// Dictionary has changed. Notify any listeners.
/// </summary>
/// <param name="e">Evevnt arguments</param>
protected virtual void OnDictionaryChanged(NotifyDictionaryChangedEventArgs e)
{
if (this.DictionaryChanged != null)
{
this.DictionaryChanged(this, e);
}
}
}
那是基础课。类中的示例方法:
/// <summary> Adds a key/value pair to the
/// System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue>
/// if the key does not already exist, or updates a key/value pair in the
/// System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue>
/// if the key already exists. </summary>
/// <param name="key"> The key to be added or whose value should be updated </param>
/// <param name="addValueFactory">The function used to generate a value for an absent key</param>
/// <param name="updateValueFactory">The function used to generate a new value for an
/// existing key based on the key's existing value</param>
/// <returns> The new value for the key. This will be either be the result of addValueFactory
/// (if the key was absent) or the result of updateValueFactory (if the key was
/// present). </returns>
public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
{
TValue value;
value = this.collectionDictionary.AddOrUpdate(key, addValueFactory, updateValueFactory);
if (this.collectionDictionary.TryGetValue(key, out value))
{
ArrayList valueList = new ArrayList() { value };
ArrayList keyList = new ArrayList() { key };
NotifyDictionaryChangedEventArgs e = new NotifyDictionaryChangedEventArgs(
NotifyCollectionChangedAction.Add,
valueList,
keyList);
this.Add(value, string.Empty);
this.OnDictionaryChanged(e);
}
return value;
}
并且不要忘记枚举器:
/// <summary> Returns an enumerator that iterates through the
/// ObservableExtendedCollection<TValue>. </summary>
/// <returns> An enumerator for the
/// underlying ObservableExtendedCollection<TKey,TValue>. </returns>
public new IEnumerator<TValue> GetEnumerator()
{
return (IEnumerator<TValue>)base.GetEnumerator();
}
/// <summary> Returns an enumerator that iterates through the
/// System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue>. </summary>
/// <returns> An enumerator for the
/// System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue>. </returns>
/// <param name="collectionFlag">Flag indicates to return the collection enumerator</param>
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator(bool collectionFlag = true)
{
return this.collectionDictionary.GetEnumerator();
}
现在显然我遗漏了一些实现,因为这最初源于我尝试(大部分成功)制作只读可观察字典。但基础确实有效。希望这有点用。