简体   繁体   English

如何使用 MVVM 在双击列表框项上触发命令?

[英]How to fire a command on double-click listbox item using MVVM?

I'm trying to launch an ICommand when the user double-clicks on a listbox item.当用户双击列表框项时,我试图启动 ICommand。 Also, I'm trying to do this using the MVVM pattern.另外,我正在尝试使用 MVVM 模式来做到这一点。

In this XAML, the key press "p" works perfectly.在这个 XAML 中,按键“p”工作得很好。 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.我已经设置了一个断点来确认“PlayVideoCommand”不是通过双击调用的。 Am I missing something or do I have to use Setter (which I'm not familiar with)?我是不是遗漏了什么,还是必须使用 Setter(我不熟悉)?

<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.双击和“p”都应该执行相同的命令。 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.我有一种预感,即 MouseBinding Command 属性不是依赖属性,但我不知道如何确认这一点。

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. 您需要将事件处理程序绑定到listboxitem。

Some ways to do it are here: Double Click a ListBox item to open a browser 有一些方法可以执行此操作: 双击ListBox项以打开浏览器

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 关于为什么MVVM中的一点代码隐藏不一定是一件可怕的事情的一些讨论: 使用MVVM从WPF ListView项目中点击双击事件

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

It seems that the ListBox doesn't handle double click on a ListBoxItem. 似乎ListBox不处理ListBoxItem上的双击。 This is a good answer: Can't bind Command to ListBox 这是一个很好的答案: 无法将Command绑定到ListBox

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:像这样将 LeftDoubleClick 手势添加到 ItemTemplate:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM