开发者

Handling Mouse events on controls with MVVM pattern - best practice -

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="{Bin开发者_如何学运维ding 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


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.


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.


XCommand Open source codeplex project has better way to deal with this event based Command/CommandParameter binding. Find here, xcommand.codeplex.com

Here is the sample code below:

<Grid>
    <TextBlock Margin="20,30,20,0" VerticalAlignment="Top" Height="80" x:Name="XTextBlock"
           Foreground="{Binding FgColor, Mode=TwoWay}"
           XCmd:MouseMove.Command="{Binding TextBlockPointerMovedCommand}"
           XCmd:MouseLeftButtonDown.Command="{Binding TextBlockPointerPressedCommand}"
           XCmd:MouseLeave.Command="{Binding TextBlockPointerExitedCommand}"    
           Text="{Binding Description, Mode=TwoWay}">
    </TextBlock>
    <Grid Grid.Column="1" Background="{Binding BgColor, Mode=TwoWay}"
          XCmd:MouseMove.Command="{Binding GridPointerMovedCommand}" 
          XCmd:MouseMove.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
          XCmd:MouseLeftButtonDown.Command="{Binding GridPointerPressedCommand}"
          XCmd:MouseLeftButtonDown.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
          >
    </Grid>
</Grid>

Hope this will be helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜