One of the most important technical advantages of WPF is data binding. Data binding, you can update the interface by manipulating the data.
Data binding is most commonly used in observablecollection<t> and dictionary<t, t> these two classes.
ObservableCollection represents a dynamic collection of data that, when added, removed, or refreshed the entire list, provides a notification that updates the display of the interface by updating the collection data.
Dictionary dictionary classes, retrieval, and data manipulation performance polarity, so a collection of configuration items is used to save it.
Therefore, we think of, there is no ObservableCollection and dictionary combination of the class, so the formation of the Observabledictionary class.
Online there are many versions of the Observabledictionary class, as far as I know, the earliest and most classic is the DR.WPF inside the ItemsControl to a dictionary, Most of the other versions are referred to this to modify (the wrong thing is that I ignorant).
The version I provided today is also referenced in other editions and DR.WPF on the web.
The definition in DR.WPF is this:
public class Observabledictionary <tkey, tvalue>: Idictionary<tkey, Tvalue>, icollection< Keyvaluepair<tkey, Tvalue>>, Ienumerable<keyvaluepair<tkey, Tvalue>>, IDictionary, ICollection, IEnumerable, ISerializable, ideserializationcallback, inotifycollectionchanged, INotifyPropertyChanged
We will be careful to find that the interface and Dictionary<tkey inherited here, Tvalue> inherited the majority of the same, just more inotifycollectionchanged, inotifypropertychanged
So, today I provide the version, directly inherit from Dictionary<tkey, Tvalue> and inotifycollectionchanged, INotifyPropertyChanged.
I have tested, no bugs, excellent performance, the following code:
public class Observabledictionary<tkey, tvalue>: Dictionary<tkey, Tvalue>, inotifycollectionchanged, INot ifypropertychanged {public observabledictionary (): Base () {} private int _index; public event Notifycollectionchangedeventhandler CollectionChanged; public event PropertyChangedEventHandler PropertyChanged; Public new KeyCollection Keys {get {return base. Keys; }} public new ValueCollection Values {get {return base. Values; }} public new int Count {get {return base. Count; }} public new TValue This[tkey key] {get {return this. GetValue (key); } set {this. SetValue (key, value); }} public TValue This[int index] {get {return this. Getindexvalue (index); } set {this. Setindexvalue (index, value); }} public new void Add (TKey kEY, TValue value) {base. ADD (key, value); This. OnCollectionChanged (New NotifyCollectionChangedEventArgs (Notifycollectionchangedaction.add, this. Findpair (key), _index)); OnPropertyChanged ("Keys"); OnPropertyChanged ("Values"); OnPropertyChanged ("Count"); } public new void Clear () {base. Clear (); This. OnCollectionChanged (New NotifyCollectionChangedEventArgs (Notifycollectionchangedaction.reset)); OnPropertyChanged ("Keys"); OnPropertyChanged ("Values"); OnPropertyChanged ("Count"); } Public new bool Remove (TKey key) {var pair = this. Findpair (key); if (base. Remove (key)) {this. OnCollectionChanged (New NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Remove, Pair, _index)); OnPropertyChanged ("Keys"); OnPropertyChanged ("Values"); OnprOpertychanged ("Count"); return true; } return false; } protected void OnCollectionChanged (NotifyCollectionChangedEventArgs e) {if (this. CollectionChanged = null) {this. CollectionChanged (this, e); }} protected void OnPropertyChanged (String propertyname) {if (this. PropertyChanged = null) {this. PropertyChanged (This, new PropertyChangedEventArgs (PropertyName)); }} #region Private method private TValue getindexvalue (int index) {for (int i = 0; I & Lt This. Count; i++) {if (i = = index) {var pair = this. ElementAt (i); Return pair. Value; }} return default (TValue); } private void Setindexvalue (int index, TValue value) {try {var pair = this. Elementatordefault (index); SetValue (pair. Key, value); } catch (Exception) {}} private TValue GetValue (TKey key) {if (base. ContainsKey (key)) {return base[key]; } else {return default (TValue); }} private void SetValue (TKey key, TValue value) {if (base. ContainsKey (key)) {var pair = this. Findpair (key); int index = _index; Base[key] = value; var Newpair = this. Findpair (key); This. OnCollectionChanged (New NotifyCollectionChangedEventArgs (Notifycollectionchangedaction.replace, Newpair, pair, index)); OnPropertyChanged ("Values"); OnPropertyChanged ("item[]"); } else { This. ADD (key, value); }} private Keyvaluepair<tkey, tvalue> Findpair (TKey key) {_index = 0; foreach (var item in this) {if (item. Key.equals (key)) {return item; } _index++; } return Default (Keyvaluepair<tkey, tvalue>); } private int IndexOf (TKey key) {int index = 0; foreach (var item in this) {if (item. Key.equals (key)) {return index; } index++; } return-1; } #endregion}
Extension aspect, you can be modified by the DR.WPF version, the more technical content and scalability of the stronger!