简体   繁体   中英

The correct way to monitor property change in WPF

I searched some code in the Internet that listen to specific property being changed on a INotifyPropertyChanged object. Most of them are in the form

notifier.PropertyChanged +=
    (s,e) => {
        if(e.PropertyName.Equals(propertyName))
            run_my_code();
    }

However, according to MSDN

The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in the PropertyChangedEventArgs.

Shall the above implementation be

notifier.PropertyChanged +=
    (s,e) => {
        if(e.PropertyName.Equals(propertyName) || string.IsNullOrEmpty(e.PropertyName))
            run_my_code();
    }

? As there are so many code did it another way, would it means I have to assume something else? I sometimes have some code that does not work when I notify property change with null or empty strings.

It really depends on what run_my_code is going to do. The PropertyChanged event simply indicates that one or more properties have changed on the object.

If run_my_code needs to make decisions based on which property has changed, then you'll want to check. If it doesn't, then you don't need to check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM