How do I raise PropertyChanged
for SomeProperty
in class B
?
This example does not compile since PropertyChanged
is not accessible this way...
public class A : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
}
public class B : A
{
private object _someProperty;
public object SomeProperty
{
get => _someProperty;
set
{
_someProperty = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SomeProperty)))
}
}
}
Solution 1:
You can use this
RaisePropertyChangedExtension
:Like this:
In my opinion this is the best solution I know so far.
Disadvantage is that you're able to raise
PropertyChanged
from another class like this:It's not good practise to raise
PropertyChanged
from other classes this way, so i'm not concerned by this disadvantage.This solution is inspired by Thomas Levesque's answer here: Simple small INotifyPropertyChanged implementation
Solution 2:
You can create a protected
RaisePropertyChanged
in the base classA
:And call the method in the derived class
B
:Disadvantage is that you have to implement the
RaisePropertyChanged
method for each new base class you're creating on the opposite you avoid the disadvantage that Solution 1 had.