11���using System ;
2+ using System . Collections ;
23using System . Collections . Generic ;
34using System . Collections . Specialized ;
45using System . ComponentModel ;
56using System . Linq ;
7+ using System . Runtime . CompilerServices ;
68using System . Text ;
79using System . Threading . Tasks ;
810using System . Windows . Threading ;
911
1012namespace Torch . Collections
1113{
1214 [ Serializable ]
13- public class ObservableDictionary < TKey , TValue > : Dictionary < TKey , TValue > , INotifyCollectionChanged , INotifyPropertyChanged
15+ public class ObservableDictionary < TKey , TValue > : ViewModel , IDictionary < TKey , TValue > , INotifyCollectionChanged
1416 {
17+ private IDictionary < TKey , TValue > _internalDict ;
18+
19+ public ObservableDictionary ( )
20+ {
21+ _internalDict = new Dictionary < TKey , TValue > ( ) ;
22+ }
23+
24+ public ObservableDictionary ( IDictionary < TKey , TValue > dictionary )
25+ {
26+ _internalDict = new Dictionary < TKey , TValue > ( dictionary ) ;
27+ }
28+
29+ /// <summary>
30+ /// Create a <see cref="ObservableDictionary{TKey,TValue}"/> using the given dictionary by reference. The original dictionary should not be used after calling this.
31+ /// </summary>
32+ public static ObservableDictionary < TKey , TValue > ByReference ( IDictionary < TKey , TValue > dictionary )
33+ {
34+ return new ObservableDictionary < TKey , TValue >
35+ {
36+ _internalDict = dictionary
37+ } ;
38+ }
39+
40+ /// <inheritdoc />
41+ public event NotifyCollectionChangedEventHandler CollectionChanged ;
42+
43+ /// <inheritdoc />
44+ public event PropertyChangedEventHandler PropertyChanged ;
45+
46+ /// <inheritdoc />
47+ public IEnumerator < KeyValuePair < TKey , TValue > > GetEnumerator ( )
48+ {
49+ return _internalDict . GetEnumerator ( ) ;
50+ }
51+
52+ /// <inheritdoc />
53+ IEnumerator IEnumerable . GetEnumerator ( )
54+ {
55+ return ( ( IEnumerable ) _internalDict ) . GetEnumerator ( ) ;
56+ }
57+
1558 /// <inheritdoc />
16- public new void Add ( TKey key , TValue value )
59+ public void Add ( KeyValuePair < TKey , TValue > item )
1760 {
18- base . Add ( key , value ) ;
61+ Add ( item . Key , item . Value ) ;
62+ }
63+
64+ /// <inheritdoc />
65+ public bool Remove ( KeyValuePair < TKey , TValue > item )
66+ {
67+ return Remove ( item . Key ) ;
68+ }
69+
70+ /// <inheritdoc />
71+ public void Clear ( )
72+ {
73+ _internalDict . Clear ( ) ;
74+ OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Reset ) ) ;
75+ OnPropertyChanged ( nameof ( Count ) ) ;
76+ }
77+
78+ /// <inheritdoc />
79+ public bool Contains ( KeyValuePair < TKey , TValue > item )
80+ {
81+ return _internalDict . Contains ( item ) ;
82+ }
83+
84+ /// <inheritdoc />
85+ public void CopyTo ( KeyValuePair < TKey , TValue > [ ] array , int arrayIndex )
86+ {
87+ foreach ( var kv in _internalDict )
88+ {
89+ array [ arrayIndex ] = kv ;
90+ arrayIndex ++ ;
91+ }
92+ }
93+
94+ /// <inheritdoc />
95+ public int Count => _internalDict . Count ;
96+
97+ /// <inheritdoc />
98+ public bool IsReadOnly => false ;
99+
100+ /// <inheritdoc />
101+ public bool ContainsKey ( TKey key )
102+ {
103+ return _internalDict . ContainsKey ( key ) ;
104+ }
105+
106+ /// <inheritdoc />
107+ public void Add ( TKey key , TValue value )
108+ {
109+ _internalDict . Add ( key , value ) ;
19110 var kv = new KeyValuePair < TKey , TValue > ( key , value ) ;
20111 OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Add , kv ) ) ;
112+ OnPropertyChanged ( nameof ( Count ) ) ;
21113 }
22114
23115 /// <inheritdoc />
24- public new bool Remove ( TKey key )
116+ public bool Remove ( TKey key )
25117 {
26- if ( ! ContainsKey ( key ) )
118+ if ( ! _internalDict . ContainsKey ( key ) )
27119 return false ;
28120
29121 var kv = new KeyValuePair < TKey , TValue > ( key , this [ key ] ) ;
30- base . Remove ( key ) ;
122+ if ( ! _internalDict . Remove ( key ) )
123+ return false ;
124+
31125 OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Remove , kv ) ) ;
126+ OnPropertyChanged ( nameof ( Count ) ) ;
32127 return true ;
33128 }
34129
130+ /// <inheritdoc />
131+ public bool TryGetValue ( TKey key , out TValue value )
132+ {
133+ return _internalDict . TryGetValue ( key , out value ) ;
134+ }
135+
136+ /// <inheritdoc />
137+ public TValue this [ TKey key ]
138+ {
139+ get => _internalDict [ key ] ;
140+ set
141+ {
142+ var oldKv = new KeyValuePair < TKey , TValue > ( key , _internalDict [ key ] ) ;
143+ var newKv = new KeyValuePair < TKey , TValue > ( key , value ) ;
144+ _internalDict [ key ] = value ;
145+ OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Replace , newKv , oldKv ) ) ;
146+ }
147+
148+
149+ }
150+
151+ /// <inheritdoc />
152+ public ICollection < TKey > Keys => _internalDict . Keys ;
153+
154+ /// <inheritdoc />
155+ public ICollection < TValue > Values => _internalDict . Values ;
156+
35157 private void OnCollectionChanged ( NotifyCollectionChangedEventArgs e )
36158 {
37159 NotifyCollectionChangedEventHandler collectionChanged = CollectionChanged ;
@@ -52,12 +174,5 @@ private void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
52174 nh . Invoke ( this , e ) ;
53175 }
54176 }
55-
56-
57- /// <inheritdoc />
58- public event NotifyCollectionChangedEventHandler CollectionChanged ;
59-
60- /// <inheritdoc />
61- public event PropertyChangedEventHandler PropertyChanged ;
62177 }
63178}
0 commit comments