How to run a command to double-click a list item using MVVM?

I try to start ICommand when the user double-clicks on a list item. Also, I'm trying to do this using the MVVM pattern.

In this XAML, pressing the p key works fine. When I double-click on a list box, the command never starts. I set a breakpoint to confirm that "PlayVideoCommand" is not called with a double click. Am I missing something or do I need to use Setter (which I am not familiar with)?

<ListBox Name="SmallVideoPreviews" Grid.Column="1" MaxHeight="965" ItemsSource="{Binding BrowseVideos}" ItemTemplate="{StaticResource BrowseTemplate}"> <ListBox.InputBindings> <KeyBinding Key="p" Command="{Binding PlayVideoCommand}" CommandParameter="{Binding ElementName=SmallVideoPreviews, Path=SelectedItem}"/> <MouseBinding Gesture="LeftDoubleClick" Command="{Binding PlayVideoCommand}" CommandParameter="{Binding ElementName=SmallVideoPreviews, Path=SelectedItem}"/> </ListBox.InputBindings> </ListBox> 

Both double-clicks and "p" must execute the same command. When using the mouse, I see that listboxitem is selected. I have a hunch that the MouseBinding Command property is not a dependency property, but I don’t know how to confirm this.

+7
source share
2 answers

What happens in your example is that the list itself responds to a double click, but only in the part of its area that is not covered by the list item.

You need the event handler to bind to the listboxitem element.

Some ways to do this: Double-click the ListBox to open a browser

And some discussion about why small code in MVVM is not necessarily terrible: Utilizing a double-click event from a WPF item list item using MVVM

More discussions: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9fb566a2-0bd6-48a7-8db3-312cd3e93340/

+10
source

The ListBox does not seem to handle double-clicking on the ListBoxItem. This is a good answer: Unable to associate command with ListBox

0
source

All Articles