I'm new to MVVM but experienced in OOP. I have an easy solution to my problem, but I'm not sure if it violates MVVM or OOP or if it is a good practice.
I have a set of ViewModel classes in my WPF project. Below is my hierarchy, with an indent representing a public property.
ApplicationViewModel
SidebarViewModel SidebarPanel
TankViewModel Tank
TankItemViewModel CurrentTankListItem
PopupViewModel CurrentPopupViewModel
bool DisplayPopup
public T CurrentSidebarListItem
{
get => _currentSidebarListItem;
set
{
_currentSidebarListItem = value;
OnPropertyChanged(nameof(_currentSidebarListItem));
this.DisplayPopup = this.CurrentSidebarListItem != null; // Problematic line
}
}
The setter of the property CurrentTankListItem needs to modify DisplayPopup of the PopupViewModel class, and the only way I can think of to do this is to pass the value of CurrentPopupViewModel from my ApplicationViewModel down through the SidebarViewModel and TankViewModel to the CurrentTankListItem property as a constructor parameter. Does passing ApplicationViewModel as a parameter in this way violate MVVM or OOP, or it is not bad practice to do this? Is there any other way I can obtain the value?
PopupViewModelsubscribe to theOnPropertyChangedevent and set itself? Or you could create a lambda to do it and set it up when you instantiate all the instances. Both techniques allow you to avoid the type-to-type dependency.