因此,如果我将父 ViewModel 的引用存储在子 ViewModel 中,那会是犯罪吗?我会违反 MVVM 规则吗?我的子视图是一个带有上下文菜单的窗口。When the appropriate menu item is selected a new child view needs to be created. 父级仅负责创建子视图。所以保持对父视图模型的引用对我有很多好处。同时我不想打破模式规则。
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;
}
}