WPF binding indexer Value change notification

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.