0

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()?)

Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129
  • Why are you using PropertyChanged.Fody when you're using MVVM Light? MVVM Light gives you the flexibility you need to write custom setters and it already handles INPC. – Keithernet May 15 '20 at 16:41
  • @Keithernet from what I’ve been able to find on MVVM Light (which is all pretty...nonexistent) it looks like I still need to use `RaisePropertyChanged` which certainly isn’t any easier than `OnPropertChanged`. If that’s no longer true I’d love to be pointed to some actual in depth MVVMLight guide. – Jonathan Tuzman May 17 '20 at 01:30
  • @Keithernet maybe because Fody can reduce lines of code.(in mvvm light you often need use some code snippets template) – Lei Yang Aug 10 '20 at 06:35

0 Answers0