tags:

views:

448

answers:

4

Hi,

What's a good method to bind Commands to Events? In my WPF app, there are events that I'd like to capture and process by my ViewModel but I'm not sure how. Things like losing focus, mouseover, mousemove, etc. Since I'm trying to adhere to the MVVM pattern, I'm wondering if there's a pure XAML solution.

Thanks!

A: 

I don't think you can use it in pure XAML, but take a look at the Delegate Command.

Gerrie Schenck
+3  A: 

In order to handle events, you must have some code that attaches itself to the event and executes your command in response. The final goal is to have in XAML:

  MouseMoveCommand="{Binding MyCommand}"

In order to achieve this you need to define an attached property for each event that you want to handle. See this for an example and a framework for doing this.

Bojan Resnik
A: 

Execute Command, Navigate Frame, and Delegating Command behaviour is a pretty good pattern. It is also can be used in the Expression Blend.

On the "best practices" side, you should think twice before converting an event to a command. Normally, command is something user does intentionaly, an event most often is just an interaction trail, and should not leave the view boundaries.

Sergey Aldoukhov
+4  A: 

Have a look at Marlon Grech's Attached Command Behaviour, it could be exactly what you're looking for

Thomas Levesque
I wouldn't use the whole library, but the theory is definitely what I'm looking for.
Steve the Plant