Is there a better way than handling CommandBindings in code behind?

I think Josh Smith created a huge confusion in his 2009 article, when he made a point that his code-behind CS files remained mostly empty. MVVM is not about not having code-behind. It is about separation of concerns.

If there is any practical rule you should follow, is to make the ViewModel view agnostic (i.e. No reference from the ViewModel to the View. Think having a second unit test implementation of a 'View' for your ViewModel).

Up vote 0 down vote favorite share g+ share fb share tw.

I have an application using MVVM. I'm trying to Intercept key presses on an MCE Remote control for Play, Pause, Stop etc.... Currently I'm using command bindings with a method in the code behind performing the related action on a media element as such Before trying to include the remote control functionality I had approx 10 view-models/views with nothing in code behind. I'm wondering if there is a better way to do this so I retain the MVVM pattern or is it perfectly acceptable/preferable to implement in this way.

EDIT - I've moved the Command Bindings from a UserControl inside a View into my MainWindow. Xaml and placed the methods into URL1. MainWindow doesn't have a view/viewmodel relationship, simply a content control with a ViewModel linked to it. In my code behind methods I'm making use of a Mediator to send messages (Play,Pause,Stop etc...) to my mediaplayerviewmodel which in turn interacts with it's respective view.

Is this a good idea or is there a better way? Wpf mvvm link|improve this question edited Feb 21 at 20:00 asked Feb 21 at 18:57Oli316 88% accept rate.

I think Josh Smith created a huge confusion in his 2009 article, when he made a point that his code-behind CS files remained mostly empty. MVVM is not about not having code-behind. It is about separation of concerns.

If there is any practical rule you should follow, is to make the ViewModel view agnostic (i.e. No reference from the ViewModel to the View. Think having a second unit test implementation of a 'View' for your ViewModel).

This "no code behind" confusion caused very odd structures just to work arround a problem that shouldn't have existed to begin with. Having code behind in the MainWindow.xaml. Cs is perfectly reasonable solution, as long as you don't have logic there, but simply forward the call to an appropriate method in the View Model.

If that was my code I would have created custom commands (a la DelegateCommand from the same article) that binds directly to commands in the ViewModel, but your design is 100% legit as well.

Actualy no, it is a limitaion of stock WPF. Stock WPF can not sensible call methods defined on a view or controller. – TomTom Feb 23 at 9:56 Not sure Why this was marked down?

I've gone with a solution based on the last paragraph. I've created custom routed commands that bubble up to mainwindow (Which has no view) but does manipulate viewmodels. – Oli Feb 27 at 11:25.

Head over to Codeplex.com and look for Caliburn (or better Caliburn Micro). It extends WPF to actually allow calling of methods with arbitrary parameters pulled from other objects and the method being in the view model / controller without having a "hook method" in code behind just fowwarding the call. The extensions can do wonderfull thignsl ike pull the value of a textbox and pass it as parameter to a method, then react on the return value - much like a view should.

You run in a limitation of "stock" wpf - which simply can only point towards method handlers in code behind without any regards to parameters. Alterantives exist, even from microsoft.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions