Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/11/10 03:55:51 (14 years ago)
Author:
swagner
Message:

Implemented INotifyPropertyChanged in all observable collections (#819)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Collections/3.3/ObservableKeyedCollectionBase.cs

    r2618 r2620  
    2424using System.Collections.Generic;
    2525using System.Collections.ObjectModel;
     26using System.ComponentModel;
    2627using System.Linq;
    2728using System.Text;
     
    9394        }
    9495      }
    95       if (!oldKeyFound) throw new ArgumentException("item not found");
     96      if (!oldKeyFound) throw new ArgumentException("Item not found");
    9697      dict.Remove(oldKey);
    9798      dict.Add(GetKeyForItem(item), item);
     99      OnPropertyChanged("Item[]");
    98100      OnItemsReplaced(new TItem[] { item }, new TItem[] { item });
    99101    }
     
    139141    public void Add(TItem item) {
    140142      dict.Add(GetKeyForItem(item), item);
     143      OnPropertyChanged("Item[]");
     144      OnPropertyChanged("Count");
    141145      OnItemsAdded(new TItem[] { item });
    142146    }
    143147    public void AddRange(IEnumerable<TItem> collection) {
    144148      if (collection == null) throw new ArgumentNullException();
    145       foreach (TItem item in collection)
     149      bool empty = true;
     150      foreach (TItem item in collection) {
    146151        dict.Add(GetKeyForItem(item), item);
    147       OnItemsAdded(collection);
     152        empty = false;
     153      }
     154      if (!empty) {
     155        OnPropertyChanged("Item[]");
     156        OnPropertyChanged("Count");
     157        OnItemsAdded(collection);
     158      }
    148159    }
    149160
     
    152163      if (TryGetValue(key, out item)) {
    153164        dict.Remove(key);
     165        OnPropertyChanged("Item[]");
     166        OnPropertyChanged("Count");
    154167        OnItemsRemoved(new TItem[] { item });
    155168        return true;
     
    159172    public bool Remove(TItem item) {
    160173      if (dict.Remove(GetKeyForItem(item))) {
     174        OnPropertyChanged("Item[]");
     175        OnPropertyChanged("Count");
    161176        OnItemsRemoved(new TItem[] { item });
    162177        return true;
     
    171186          items.Add(item);
    172187      }
    173       if (items.Count > 0)
     188      if (items.Count > 0) {
     189        OnPropertyChanged("Item[]");
     190        OnPropertyChanged("Count");
    174191        OnItemsRemoved(items);
     192      }
    175193    }
    176194    public int RemoveAll(Predicate<TItem> match) {
     
    181199
    182200    public void Clear() {
    183       TItem[] items = dict.Values.ToArray();
    184       dict.Clear();
    185       OnCollectionReset(new TItem[0], items);
     201      if (dict.Count > 0) {
     202        TItem[] items = dict.Values.ToArray();
     203        dict.Clear();
     204        OnPropertyChanged("Item[]");
     205        OnPropertyChanged("Count");
     206        OnCollectionReset(new TItem[0], items);
     207      }
    186208    }
    187209    #endregion
     
    257279        CollectionReset(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
    258280    }
     281
     282    [field: NonSerialized]
     283    public event PropertyChangedEventHandler PropertyChanged;
     284    protected virtual void OnPropertyChanged(string propertyName) {
     285      if (PropertyChanged != null)
     286        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     287    }
    259288    #endregion
    260289  }
Note: See TracChangeset for help on using the changeset viewer.