This project is read-only.

How can I interact with a View from my ViewModel? Like how do I close a Window?


Easy enough. Does this break our MVVM paradigm?  Sure does, just a little. In reality, your ViewModel still doesn't really know what the View is. It is very easy to mock the View for testing, and you aren't really depending on any aspect of the View other than there is a method named "Close" with no parameters.

Interacting with the view in this way should be used with extreme caution. Don't be adding child controls, applying styles, etc. It is meant to get you out of some tight MVVM spots and nothing more.

How else is it useful? What if you need to use a crappy third party control that has no ICommands and requires calling methods directly. "This.View" sure fits the bill here.  Otherwise you could subclass the control and implement ICommand to fix it.  But what if it's sealed?  "This.View" can be a lifesaver.

Down the road, new features should surface to where you won't need to use "This.View" as often.

Last edited Aug 4, 2011 at 3:24 AM by jonathanpeppers, version 5


No comments yet.