The primary metric is: can you test it?
The view absolutely needs a reference to the view model. How else would it get the information it needs to display itself? Usually just having the view model in the DataContext
of the view is sufficient, but that still constitutes a dependency. Without the VM in the DataContext
, the view would be useless.
Sometimes you need the view to call back to the VM. Normally I just do this in my code behind:
public MyViewModel ViewModel
{
get { return DataContext as MyViewModel; }
}
private void _someEventHandler(object sender, EventArgs )
{
ViewModel.SomeMethod();
}
Holding a reference to the view from the view model is where you need to be careful. If the VM depends on a particular view implementation, it is tightly coupled to the view and difficult to test as a result.
If the VM needs to interact in some fashion with the view, you can abstract the requirements into an interface and have the view implement that interface. Then, when the view loads, it can provide the VM with a reference to itself with which the VM can interact.
HTH,
Kent