/// <summary> /// Gets or sets the element with the specified key. /// </summary> /// <returns> /// The element with the specified key. /// </returns> /// <param name = "key">The key of the element to get or set.</param> /// <exception cref = "T:System.ArgumentNullException"><paramref name = "key" /> is null.</exception> /// <exception cref = "T:System.Collections.Generic.KeyNotFoundException">The property is retrieved and <paramref name = "key" /> is not found.</exception> /// <exception cref = "T:System.NotSupportedException">The property is set and the <see cref = "T:System.Collections.Generic.IDictionary`2" /> is read-only.</exception> public TValue this[TKey key] { get { TValue value; TryGetValue(key, out value); return(value); } set { var oldItem = new KeyValuePair <TKey, TValue>(key, this[key]); var newItem = new KeyValuePair <TKey, TValue>(key, value); var e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Replace, newItem, oldItem); OnDictionaryChanging(e); Dictionary[key] = value; OnDictionaryChanged( new NotifyDictionaryChangedEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Replace, newItem, oldItem)); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItem, oldItem)); } }
/// <summary> /// Raises the <see cref = "DictionaryChanging" /> event. /// </summary> /// <param name = "e">The <see cref = "Dnp.Collections.NotifyDictionaryChangingEventArgs<TKey,TValue>" /> instance containing the event data.</param> protected void OnDictionaryChanging(NotifyDictionaryChangingEventArgs <TKey, TValue> e) { DictionaryChangingEventHandler <TKey, TValue> handler = DictionaryChanging; if (handler != null) { handler(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() { var e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Reset); OnDictionaryChanging(e); Dictionary.Clear(); OnDictionaryChanged(new NotifyDictionaryChangedEventArgs <TKey, TValue>( NotifyCollectionChangedAction.Reset)); OnCollectionChanged(new NotifyCollectionChangedEventArgs(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) { var e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Add, key, value); OnDictionaryChanging(e); Dictionary.Add(key, value); var item = new KeyValuePair <TKey, TValue>(key, value); OnDictionaryChanged(new NotifyDictionaryChangedEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Add, item)); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); }
/// <summary> /// Adds a range of items to this instance. /// </summary> /// <param name = "dictionary">The dictionary.</param> public void AddRange(IDictionary <TKey, TValue> dictionary) { var e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Add, dictionary); OnDictionaryChanging(e); foreach (KeyValuePair <TKey, TValue> pair in dictionary) { Dictionary.Add(pair.Key, pair.Value); } OnDictionaryChanged(new NotifyDictionaryChangedEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Add, dictionary)); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, dictionary)); }
/// <summary> /// Removes the range of items specified. /// </summary> /// <param name = "dictionary">The dictionary.</param> public void RemoveRange(IDictionary <TKey, TValue> dictionary) { var e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Remove, dictionary); OnDictionaryChanging(e); foreach (TKey local in dictionary.Keys) { Dictionary.Remove(local); } OnDictionaryChanged( new NotifyDictionaryChangedEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Remove, dictionary)); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, dictionary)); }
/// <summary> /// Removes the element with the specified key from the <see cref = "T:System.Collections.Generic.IDictionary`2" />. /// </summary> /// <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> /// <param name = "key">The key of the element to remove.</param> /// <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 (!Keys.Contains(key)) { return(false); } TValue local = this[key]; var item = new KeyValuePair <TKey, TValue>(key, local); var e = new NotifyDictionaryChangingEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Remove, item); OnDictionaryChanging(e); bool flag = Dictionary.Remove(key); OnDictionaryChanged( new NotifyDictionaryChangedEventArgs <TKey, TValue>(NotifyCollectionChangedAction.Remove, item)); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); return(flag); }