示例#1
0
 /// <summary>
 ///   Adds an item to the dictionary.
 /// </summary>
 /// <param name = "item">The item to add.</param>
 public void Add(ObservableKeyValuePair <TKey, TValue> item)
 {
     if (this._BaseDictionary.ContainsKey(item.Key))
     {
         throw new ArgumentException("Dictionary already contains key.");
     }
     this._BaseDictionary[item.Key] = item.Value;
     this._BaseCollection.Add(item);
     this._IndexDictionary[item.Key] = this._BaseCollection.Count - 1;
     this.Subscribe(item);
     this.OnPropertyChanged("Count");
     this.OnPropertyChanged("Item[]");
     this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,
                                                                   item,
                                                                   this._IndexDictionary[item.Key]));
 }
示例#2
0
        /// <summary>
        ///   Removes an item from the dictionary.
        /// </summary>
        /// <param name = "item">The item to remove.</param>
        /// <returns>true if the item was removed.  false otherwise.</returns>
        public bool Remove(ObservableKeyValuePair <TKey, TValue> item)
        {
            if (!this.Contains(item))
            {
                return(false);
            }
            int index = this._IndexDictionary[item.Key];

            this._BaseCollection.RemoveAt(index);
            for (int x = index; x < this._BaseCollection.Count; x++)
            {
                this._IndexDictionary[this._BaseCollection[x].Key] = x;
            }
            this._BaseDictionary.Remove(item.Key);
            this._IndexDictionary.Remove(item.Key);
            this.Unsubscribe(item);
            this.OnPropertyChanged("Count");
            this.OnPropertyChanged("Item[]");
            this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,
                                                                          item,
                                                                          index));
            return(true);
        }
示例#3
0
 private void Subscribe(ObservableKeyValuePair <TKey, TValue> item)
 {
     item.PropertyChanged += this.ItemPropertyChanged;
 }
示例#4
0
 /// <summary>
 ///   Checks to see whether the item is in the dictionary.
 /// </summary>
 /// <param name = "item">The item to search for.</param>
 /// <returns>true if the item is in the dictionary.  false otherwise.</returns>
 public bool Contains(ObservableKeyValuePair <TKey, TValue> item)
 {
     return(this._BaseCollection.Contains(item));
 }