简体   繁体   中英

WPF - PropertyChanged EventHandler not firing

I'm having a problem with subscribing to PropertyChangedEventHandler event of a property on a bound instance of my class.

Here is the setup:

XAML:

<CheckBox IsChecked="{Binding MyObservableClassInstance.BooleanProperty}"/>

DataContext class property:

public MyObservableClass MyObservableClassInstance 
{
    get { return _myClassInstance; }
    set
    {
        _myClassInstance= value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MyObservableClassInstance"));
    }
}

the initialisation and subscription to PropertyChanged event (this subscribed method is never reached):

MyObservableClassInstance = new MyObservableClass();
MyObservableClassInstance.PropertyChanged += OnMyObservableClassPropertyChanged; // <--- This method is never hit

my observable class: (the BooleanProperty is working normally with the the XAML checkbox binding)

public class MyObservableClass : INotifyPropertyChanged
{
    bool _mybool = false;
    
    public event PropertyChangedEventHandler PropertyChanged;

    public bool BooleanProperty
    {
        get { return _mybool; }
        set
        {
            _mybool = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("BooleanProperty")); // <--- This is reached normally on checking/unchecking the checkbox
        }
    }
}

So why is my OnMyObservableClassPropertyChanged method never reached upon Invoking the PropertyChanged event?

You have to attach (and detach) the handler method to the PropertyChanged event whenever the MyObservableClass property value changes.

public MyObservableClass MyObservableClassInstance 
{
    get { return _myClassInstance; }
    set
    {
        if (_myClassInstance != null)
        {
            _myClassInstance.PropertyChanged -= OnMyObservableClassPropertyChanged;
        }

        _myClassInstance = value;

        if (_myClassInstance != null)
        {
            _myClassInstance.PropertyChanged += OnMyObservableClassPropertyChanged;
        }

        PropertyChanged?.Invoke(
            this, new PropertyChangedEventArgs(nameof(MyObservableClassInstance)));
    }
}

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