简体   繁体   中英

INotifyPropertyChanged Event Not Calling Event Handler

I'm doing a bit of MVVM work in SL 4.0, and I've got an event handler that never gets called, despite the event firing, and being non-null.

First, I attach the event handler. I stepped through this, and notice that after adding the event handler, that PropertyChanged is no longer null, HOWEVER, it's invocationCount is still 0, and it's invocationList is still null. That's not expected right?

node.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(OnMonitoredDataSourceNodePropertyChanged);

Next, I change a property, and call my OnPropertyChanged method, like so:

OnPropertyChanged("CheckState");

Which fires the method. I step through that, and upon inspecting the PropertyChanged event, it is NOT NULL which is expected, but, it has an invocationCount of 3, and the invocationList has 4 objects, the last of which is NULL. That makes no sense to me, there should only be the 1 handler that was previously assigned. Instead, its some other list, and one that does not include my original handler.

public void OnPropertyChanged(string propertyName) {
        if (this.PropertyChanged != null) {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

So, PropertyChanged is fired, but my handler is never called. I've been beating my head against a wall for a couple of hours on this one, any help is appreciated.

There is nothing wrong with your code as it appears in the question. It would suggest that 1) your code is not as is represented here, or 2) the instance you add the listener to in the first part is not the instance you are firing OnPropertyChanged on. Number 2 is my bet.

In order to test this, you should debug your application, placing a breakpoint where you add a listener, and where you are firing OnPropertyChanged .

Within Visual Studio, when the first breakpoint is hit, make an object ID on the instance referenced by the node variable. Let your application continue execution.

When the second breakpoint hits in OnPropertyChanged , examine the object ID on this . You'll find out whether or not the instances are the same.

In all things debugging, determine what your assumptions are. Because that's where your bug lies.

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