One of the main tenants of the MVVM template is that you should be able to execute your ViewModel code without a view so that unit test is your view logic. In words, ideally, you should be able to run your application in headless mode.
In your example, you indicate that ParentView creates a ChildView, which in turn creates a ChildViewModel (which you are trying to connect). Can this work in headless mode? It seems to me that you rely on your appearance to perform this navigation with your parent child.
If you change it in another way, create a ParentViewModel to create a ChildViewModel, you will no longer have a problem communicating between ViewModels. ParentView must "watch" (i.e., Change the property) for the new ChildViewModel to be created and accordingly create the ChildView.
More details:
- ParentView instantiates ParentVM
- The user interacts in such a way that a child is required.
- ParentVM creates ChildVM by exposing it using the ChildVM property
- ParentView handles the resulting PropertyChanged event, creating a ChildView, setting its DataContext for ChildVM.
source share