Refactor Logic into a ViewModelBase Class
Move the INotifyPropertyChanged
interface into a base class so that it can be moved into other ViewModels:
MainViewModel.cs
public class MainViewModel : INotifyPropertyChanged { … }
ViewModelBase.cs
public class ViewModelBase : INotifyPropertyChanged
{
// Moved from MainViewModel:
public event PropertyChangedEventHandler? PropertyChanged;
// Moved from MainViewModel:
// Change access modifier to protected to call this from subclasses:
protected virtual void RaisePropertyChanged(string? propertyName = null)
{
// The sender is this MainViewModel instance;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
MainViewModel.cs
public class MainViewModel : ViewModelBase { … }