10

I'm trying to launch an ICommand when the user double-clicks on a listbox item. Also, I'm trying to do this using the MVVM pattern.

In this XAML, the key press "p" works perfectly. When I double click on the list box, the command never starts. I've set a break point to confirm "PlayVideoCommand" is not called with a double-click. Am I missing something or do I have to use Setter (which I'm 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-click and "p" should execute the same command. When using the mouse, I can see the 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.

James
  • 954
  • 2
  • 13
  • 27

3 Answers3

13

What's happening in your sample is that the listbox itself is reacting to the double click, but only in the part of it's area that is not covered by a list box item.

You need the event handler to be tied to the listboxitem.

Some ways to do it are here: Double Click a ListBox item to open a browser

And some discussion about why a little code-behind in MVVM is not necessarily a terrible thing: Firing a double click event from a WPF ListView item using MVVM

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

Community
  • 1
  • 1
Japple
  • 965
  • 7
  • 14
  • I saw the second link you posted, but was hoping things have changed since it was posted a few years ago. I think I'm going to post a couple of lines in the code behind file. I'm having a hard time sticking with MVVM on this one. Thanks for the detail. – James Jun 23 '12 at 22:08
1

It seems that the ListBox doesn't handle double click on a ListBoxItem. This is a good answer: Can't bind Command to ListBox

Community
  • 1
  • 1
MBen
  • 3,956
  • 21
  • 25
0

One could argue weather or not code-behind is terrible, but it ís possible use a command. Add the LeftDoubleClick gesture to the ItemTemplate like this:

<UserControl.Resources>
    <DataTemplate x:Key="BrowseTemplate" >
        <StackPanel >
            <StackPanel.InputBindings>
                <MouseBinding Gesture="LeftDoubleClick"
                              Command="{Binding DataContext.PlayVideoCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Mode=OneWay}" 
                              CommandParameter="{Binding }" />
            </StackPanel.InputBindings>
            <TextBlock Text="{Binding }" Width="50" />
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>
ffonz
  • 1,304
  • 1
  • 11
  • 29