O'Reilly logo

Microsoft Silverlight 5 Data and Services Cookbook by Kevin Dockx, Gill Cleeren

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using commands to pass your events to the ViewModel

Applies to Silverlight 3, 4, 5 and WP7

Up until now, we've passed through click events (or other events) by setting up a handler in the code behind of our View. In this view, we then cast the DataContext to a specific ViewModel type, which contains the implementation of the handler.

This is easy, but it's not the way to go in MVVM. By doing this, you're adding in some extra tight coupling between your View and ViewModel, which is exactly what MVVM tries to avoid. A better way to tackle this problem is by using commands. This will allow you to bind an event on your View to a command on your ViewModel, thus eliminating the need to pass through events via the code behind your View.

In this recipe, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required