Handling mouse events on controls with the MVVM pattern - best practice -

I actually found 2 ways to handle mouse events on controls using the mvvm template.

Both methods are actually one way:

MVVM Light Toolkit from 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 blend interactiveivity.dll with behavior

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

Do you know any better method?

Moderator: Why are my last 6 xaml lines of code not visible at all? They are swallowed by IE and the Iron browser. Could you tell the administrator to fix this script code? it doesnโ€™t work very often at all. prove: http://img251.imageshack.us/img251/5236/errorxt.png

+6
events command wpf mvvm mouse
source share
3 answers

These are good ways to do this if you need to handle MouseDown in arbitrary places.

However, these situations, as a rule, are few and far from each other. There is usually an easier way:

  • Are you sure your objects are not really buttons that just don't look like buttons? If so, make them real Button objects and create them to look the way you want.
  • Are you sure that your objects are only areas of selection of objects in the list? If so, change the container from ItemsControl to ListBox and Restyle ListBoxItem to use selection areas.
  • Are your objects the graphic paths that are selected? Use a ToggleButton whose contents are the path itself.

There are many other examples of this. In fact, it is rare that MouseDown displays a command, and there is no cleaner way to do the same.

+8
source share

There is always another option. You can handle WPF events in View code and call the appropriate method in the ViewModel. The MVVM pattern does not prohibit writing any code in a file with a reverse view code.

Application ViewModel Application

+7
source share

XCommand In an open source codeplex project, there is a better way to handle the Command / CommandParameter binding based on this event. 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.

+2
source share

All Articles