Background
In some applications, the interface needs to be bound to the indexer and updated in real time as the value changes.
Solution Solutions
Simply implement the INotifyPropertyChanged interface with the class that contains the indexer and raise the PropertyChanged event when the index value changes, and set the property name to item[]. The sample code is as follows:
Public classnotifydictionary: INotifyPropertyChanged{ Private ReadOnlyDictionary<string, string> _dictionary =NewDictionary<string, string> (); Public String this[stringIndex] { Get{ if(_dictionary. ContainsKey (Index)) {return_dictionary[index]; } return String. Empty; } Set{ stringOldValue =string. Empty; if(_dictionary. ContainsKey (Index)) {oldValue = _dictionary[index]; } _dictionary[index] =value; if(oldValue! =value) {onpropertychanged (Binding. IndexerName); } } } #regionINotifyPropertyChangedPublic EventPropertyChangedEventHandlerpropertychanged; protected virtual voidonpropertychanged (stringPropertyName) { if(propertychanged! =NULL) {propertychanged ( This, NewPropertyChangedEventArgs(PropertyName)); } } #endregionINotifyPropertyChanged}
In the code, Binding.indexername is a constant, and the value is item[].
Principle
In essence, an indexer is also an attribute, that is, a parameter attribute.
In the WPF binding system, the property Change event is monitored, and if the changed property name is item[] and the indexer is bound, the boundary value is updated. As you can see, this process is not related to the actual indexer name. Therefore, even using indexernameattribute to explicitly change the indexer name on the indexer does not affect the entire process.
No parameter attribute binding cold knowledge
In the WPF binding system, the property Change event is monitored. However, if the bound property is a non-parameter property (that is, a normal property, a non-indexer), its comparison with the changed property names is case-insensitive. So in the code below,
_name; name{ return this. _name;} Set { value) { value; This . OnPropertyChanged ("naMe"); }}}
The name, name, name, and name effect are the same.
Code download
Blog Park: Notifydictionarydemo
WPF binding indexer Value change notification