Requirements to use EventToCommand

Mar 31, 2010 at 6:42 PM
I was building a demo app for wiring up a calendar to a command in a existing silverlight bussiness application (SL+wcfRia+EF), wich used a self developed set of clases that I don't dare to call framework. I added reference to the required libraries, correct versions (SL4) and created the command on the viewmodel, wired all up in the xaml, but it didn't worked. So after a while of errors and frustrations, decided to try this on a brand new proyect using the provided proyect template, and it did work. So my question is, ¿Does the viewmodel has to inherit the mvvmlight viewmodelbase in order to work with this set of classes?
Coordinator
Apr 5, 2010 at 7:14 PM

Hi,

Sorry for the delay.

No, RelayCommand can be used on any class, as long as you make sure that you are using the right version. You didn't mention if you were using EventToCommand to do the link. The Blend behavior syntax can be tricky if you don't create the element in Blend. That said, it should work. There must be a small issue somewhere that was overlooked.

Greetings,

Laurent

Apr 6, 2010 at 4:56 PM

Doesn't the use of EventToCommand and then passing the EventArgs to the VM violate the separate of knowledge rule?  Once the VM has a reference to a type such as SelectionChangedEventArgs, then the VM has explicit knowledge of the View and it's constituent controls.  As far as I know, shouldn't each actor in the MVVM triad be agnostic to it's fellow actor?

Coordinator
Apr 7, 2010 at 4:32 PM

Hi,

Yes it absolutely does violate it, but at least it does so consciously :) This is one of these cases where we are limited by the framework (i.e. the fact that there is so much information contained in the EventArgs instance, and that it is in fact a UI object).

Personally I think it is OK to do so, as long as we are aware of the issue (for example that mocking an EventArgs can be difficult, depending on the instance), because the alternative is to add some code-behind to subscribe to the event, and then pass the EventArgs to the VM.

But yeah you're absolutely right, it is a compromise and it does violate the purity of the VM.

Cheers,

Laurent

Apr 7, 2010 at 6:31 PM

I have no issue wiring up some code-behind in the xaml.cs file.  I just pulled the VM from the DataContext and set the property as needed.

Coordinator
Apr 7, 2010 at 6:34 PM

Exactly. That is cleaner, so feel free to do so. 

Cheers,

Laurent