i'm new to WPF and MVVM but i trying to do a simple project. My application basically consist in one window with two TabItems and a common progress bar; inside each TabItem there are some control for FTP communication or similar. For its semplicity there is no need making it complicated with multiple views but i would like to make it cleanest as possible. My idea is to create three ViewModel, one for each TabItem and the other for the "common control", like the progress bar (used for the FTP transfer progress) and be able to access from TabItem1VM and TabItem2VM to CommonVM. I have read that a good approach could be using a singleton CommonVM, but how i deal with singleton if i want to implement INotifyPropertyChanged for the Progress bar property and some few more?
At the moment i sketched something like this, but i think is quite a mess:
internal class Notifier : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
internal class ViewModelCommon : Notifier
{
public static ViewModelCommon Instance = new ViewModelCommon();
public bool OperationInProgress
{
get { return GetInstance().OperationInProgress; }
set {
GetInstance().OperationInProgress = value;
OnPropertyChanged("OperationInProgress");
}
}
private ViewModelCommon() { }
internal static ViewModelCommon GetInstance()
{
if(Instance == null )
{
Instance = new ViewModelCommon();
}
return Instance;
}
}
internal class ViewModelBase : Notifier
{
public ViewModelCommon VMCommmon
{
get { return ViewModelCommon.GetInstance(); }
}
}
internal class ViewModel1 : ViewModelBase
{
}
internal class ViewModel2 : ViewModelBase
{
}