views:

292

answers:

2

Hello my mvvm followers :D

I found actually 2 ways to handle mouse events on controls with the mvvm pattern.

Both ways are actually 1 way:

MVVM Light Toolkit by http://mvvmlight.codeplex.com/

<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <cmd:EventToCommand
            Command="{Binding SelectionChangedCommand}"
            CommandParameter="{Binding SelectedItems,
                ElementName=MyDataGrid}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

and the Blend interactivity.dll with Behaviours

<i:Interaction.Triggers>
  <i:EventTrigger EventName=”MouseLeftButtonDown”>
    <Behaviours:ExecuteCommandAction Command=”{Binding MyCommand}” CommandParameter=”{Binding MyCommandParameter}”/>
  </i:EventTrigger>
</i:Interaction.Triggers>

Do you know of any better method?

Moderator: Why the heck are my last 6 xaml lines of code not visible at all? They are swallowed by IE and Iron browser. Would you please report the admin to fix that code script? its not working at all very often. prove: http://img251.imageshack.us/img251/5236/errorxt.png

+1  A: 

Those are both good ways to do it if you need to handle MouseDown in arbitrary places.

However these situations generally are few and far between. Usually there is a simpler way:

  • Are you sure your objects aren't really buttons that just don't look like buttons? If so, make them real Button objects and template them to look the way you want.
  • Are you sure your objects are just selection areas for objects in a list? If so, change the container from ItemsControl to ListBox and restyle ListBoxItem to use the selection areas.
  • Are your objects graphical paths that are being selected? Use a ToggleButton whose content is the path itself.

There are many other examples of this. In fact, it is uncommon to find a situation in which a MouseDown maps to a Command and there isn't a cleaner way to do the same thing.

Ray Burns
You mention MouseDown explecitly and often, what about Mouse clicks? are the above methods not good for it? I see often mouse clicks with attached behaviours regarding mvvm and events. But those I would have to code formyself...
msfanboy
How do you define "mouse clicks"? In WPF, this concept doesn't really exist. You have button clicks, but there is no such thing as mouse "clicks": The mouse button going down and up are separate events. For button clicks, you don't need a way to map the Click event to a command because the Button class already has a Command property that you can use directly.
Ray Burns
sorry it seems my code snippets were misleading: MouseLeftButtonDownI never wanted to speak about buttons... I know they have a Command property to bind to...
msfanboy
+1  A: 

There is always another option. You can handle WPF events in the code-behind of the View and call the appropriate method on the ViewModel. The MVVM pattern doesn't forbid to write any code in the code-behind file of the View.

The ViewModel sample application of the WPF Application Framework (WAF) shows how this can work.

jbe