/// <summary> /// Raises the <see cref="DictionaryChanging" /> event. /// </summary> /// <param name="e"> /// The <see cref="System.Collections.NotifyDictionaryChangingEventArgs<TKey,TValue>" /> instance /// containing the event data. /// </param> protected virtual void OnDictionaryChanging(NotifyDictionaryChangingEventArgs <TKey, TValue> e) { EventHandler <NotifyDictionaryChangingEventArgs <TKey, TValue> > eventHandler = this.DictionaryChanging; if (eventHandler != null) { eventHandler(this, e); } }
/// <summary> /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1" />. /// </summary> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only. /// </exception> public void Clear() { NotifyDictionaryChangingEventArgs <TKey, TValue> e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(this.Dictionary, NotifyCollectionChangedAction.Reset); this.OnDictionaryChanging(e); if (e.Cancel) { return; } this.Dictionary.Clear(); this.OnDictionaryChanged(new NotifyDictionaryChangedEventArgs <TKey, TValue>(Dictionary, NotifyCollectionChangedAction.Reset)); }
/// <summary> /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> /// <param name="key">The object to use as the key of the element to add.</param> /// <param name="value">The object to use as the value of the element to add.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.Generic.IDictionary`2" /> is read-only. /// </exception> public void Add(TKey key, TValue value) { NotifyDictionaryChangingEventArgs <TKey, TValue> e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(key, value, NotifyCollectionChangedAction.Add); this.OnDictionaryChanging(e); if (e.Cancel) { return; } this.Dictionary.Add(key, value); this.OnDictionaryChanged(new NotifyDictionaryChangedEventArgs <TKey, TValue>(key, value, NotifyCollectionChangedAction.Add)); }
/// <summary> /// Gets or sets the value with the specified key. /// </summary> /// <param name="key">The key.</param> /// <returns> /// Returns the value for the key. /// </returns> public TValue this[TKey key] { get { return(this.Dictionary[key]); } set { var oldItem = new KeyValuePair <TKey, TValue>(key, this[key]); var newItem = new KeyValuePair <TKey, TValue>(key, value); NotifyDictionaryChangingEventArgs <TKey, TValue> e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(newItem, oldItem, NotifyCollectionChangedAction.Replace); this.OnDictionaryChanging(e); if (e.Cancel) { return; } this.Dictionary[key] = value; this.OnDictionaryChanged(new NotifyDictionaryChangedEventArgs <TKey, TValue>(newItem, oldItem, NotifyCollectionChangedAction.Replace)); } }
/// <summary> /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </summary> /// <param name="key">The key of the element to remove.</param> /// <returns> /// true if the element is successfully removed; otherwise, falSE. This method also returns false if /// <paramref name="key" /> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2" />. /// </returns> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key" /> is null. /// </exception> /// <exception cref="T:System.NotSupportedException"> /// The <see cref="T:System.Collections.Generic.IDictionary`2" /> is read-only. /// </exception> public bool Remove(TKey key) { if (!this.ContainsKey(key)) { return(false); } TValue value = this[key]; NotifyDictionaryChangingEventArgs <TKey, TValue> e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(key, value, NotifyCollectionChangedAction.Add); this.OnDictionaryChanging(e); if (e.Cancel) { return(false); } this.Dictionary.Remove(key); this.OnDictionaryChanged(new NotifyDictionaryChangedEventArgs <TKey, TValue>(key, value, NotifyCollectionChangedAction.Remove)); return(true); }