Alex Rivera | Logout

MVVM View reference to ViewModel

Asked 2010-03-29T16:30:36.837
13

I'm using MVVM in a WPF app. I'm very new to both. Let me state that I am not a purist in the MVVM pattern, I am trying to use as many best practices as I can but am trying to make what I think are reasonable compromises to make it work in our environment. For example, I am not trying to achieve 0% code in my View code-behind.

I have a couple of questions about best practices.

1) I understand I don't want my VM to know about the attached View, but is it reasonable for the View to have a reference to its VM?

2) If a control in a View opens another View (such as a dialog) should I handle this in the View? It seems wrong to handle it in the VM since then the VM has some knowledge of a specific View.

Edit
Report

2 Answers

2

1). The view will need a reference to the view model at some level, since the viewmodel will act as the view's datacontext.

2) One way to handle this is to have a generalized viewmodel representing a dialog, that is owned by the main viewmodel (the one being used as the views datacontext.)

You can use a command to crate a new instance of a dialog viewmodel, which will have a corresponding datatemplate defined in your resources. This template will be set to bind to the dialogviewmodel type.

answered 2010-03-29T16:48:46.230
1

Build Your Own MVVM Framework

I found the approach suggested by Rob Eisenberg very interesting.

Key points:

  1. Convention over configuration
  2. ViewModel first

Which is very similar to ASP.NET MVC philosophy.

I highly recommend watching the video.

answered 2010-04-01T12:13:43.877

Your Answer