So, if I save the parent link of the ViewModel in the child view of the ViewModel, would that be a crime? Am I breaking the rules of MVVM? My child view is a window with a context menu. When the corresponding menu item is selected, it is necessary to create a new child view. Only the parent is responsible for creating the child view. Therefore, keeping a reference to the parent view model will do me a lot of good. At the same time, I do not want to break template rules.
class MainViewModel { List<ChildViewModel> _childrenViewModels = new List<ChildViewModel>(); public AddChild(ChildViewModel childViewModel) { _childrenViewModels.Add(childViewModel); childViewModel.Owner = this; } } class ChildViewModel { private Child _child; public MainViewModel Owner { get; set; } public ChildViewModel(Child child) { _child = child; } }
source share