WPF Routed Commands + MVVM

A lot of the MVVM guidance around commands suggest using a generic ICommand implementation, such as the RelayCommand or GenericCommand implementations. These are great for UI pieces that are bound directly to an underlying ViewModel and I use them frequently.

There are still cases where a RoutedUICommand is exactly what one needs. Take, for instance, toolbar buttons or menu items that need to act on pages that are loaded in a WPF frame. In this case, a RoutedUICommand is appropriate since it is decoupled from the page, which may or may not be loaded at the time.

One challenge with MVVM based scenarios is connecting a routed command to an implementation that lives in the ViewModel. As usual, Josh Smith has an solution… Josh wrote a ViewModel Commanding sample back in 2008 and posted it to CodeProject.

It took me a little bit of review to understand the way it works, but the end result is a very clean and simple implementation that stays close to the original design intent behind WPF’s routed commands.

Thanks Josh!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: