General MVVM Philosophy/Thoughts - Your Feedback

Topics: Prism v2 - WPF 3.5
Mar 26, 2010 at 8:25 PM

For a WPF application using Prism and MVVM, what are folks thoughts on handling what really are UI events?

For example, sorting.  Should the event handler for sorting be in the code-behind or in the view model?  So, we would either have code-behind code that references the view model property or we would have, say, a DelegateCommand in the view model which the view binds to which then leads to updating the property. 

In previous MVVM applications, the mandate set from on high (read: the in-house architect) was to have NO code in the code-behind.  I have also done apps where there was no such mandate so we did some UI event handling in the code-behind.  I realize there is probably not a right answer, but in developing a new WPF Prism app that I am currently working on, I thought I'd get folks opinions.

So, your thoughts? 

Mar 26, 2010 at 9:14 PM

Hi Gorter

Everything you can do in the code behind can be done with Attached property and DelegateCommand. Note that the MVVM patern doesn't mean zero "code" in the code-behind but less code.

The philosophy of the MVVM is to get benefits from the binding and ensure a better collaboration between the developers and the designers. I've written a lot of viewmodels

in my last project with no code in my code-behing. But it depends on how you feel with MVVM or PM or any other patern.

Try to google a little bit on MVVM. There are a lot of articles and blogs that address this patern.

Good luck

Mar 26, 2010 at 9:32 PM

As I stated, I too have done this both ways.  I'm just trying gauge other people's opinion on how things SHOULD be done, in their opinion.  I've read tons of articles on MVVM/PM, but I still don't see much consistency for using the code-behind or not.  It's always the developers choice, etc.   I just want to hear about other developers opinions. 

Mar 27, 2010 at 2:21 AM

My opinion: when using MVVM then I aim for no code-behind. BUT if I need to have a little, I wont stress about it :).

I am developing an app that is targeting SL and Windows Mobile. There are shared VMs but not all events are easily transferred between them and become platform dependent and therefore live in the code-behind files (although I have also used platfrorm specific partial classes to handle some scenarios).

*shrug* like most things it's what works that counts :)