I had been using my own ViewModelBase
and RelayCommand
, and recently I installed MVVMLight, as well as PropertyChanged.Fody. It appears that PropertyChanged.Fody doesn't notify if you're using a custom setter, so I do need to implement OnPropertyChanged
somewhere myself.
public NotifiableViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
// store the previous viewmodel when the viewmodel is changed.
PreviousViewModel = _currentViewModel;
_currentViewModel = value;
OnPropertyChanged();
}
}
This is a member of a class that still inherits from my own ViewModelBase
public abstract class NotifiableViewModelBase : INotifyPropertyChanged
{
protected NotifiableViewModelBase() { }
public virtual string DisplayName
{
get;
protected set;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
I'd like to have this class inherit from MVVMLight's ViewModelBase
, but if I do that, it says PropertyChanged
hides its inherited member, and when I delete that event's declaration (to use the inherited member), I get errors in the OnPropertyChanged
method, saying PropertyChanged
can only be used on the left hand side of a += or -=.
I've managed to get around this by declaring PropertyChanged
as a new event
, which allows me to inherit from MVVMLight's ViewModelBase
, but this smells improper to me.
Am I "doing it right" using the new event
keywords, or is there some answer to how to use PropertyChanged
as expected in the OnPropertyChanged
?
(or, is there a way to write my custom setter and have the notification work without explicitly calling OnPropertyChanged()
?)