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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11172443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:52:27  来源:igfitidea点击:

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

c#wpfmvvmdependency-properties

提问by James

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.

当用户双击列表框项时,我试图启动 ICommand。另外,我正在尝试使用 MVVM 模式来做到这一点。

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

在这个 XAML 中,按键“p”工作得很好。当我双击列表框时,命令永远不会启动。我设置了一个断点来确认“PlayVideoCommand”不是通过双击调用的。我是不是遗漏了什么,还是必须使用 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. 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.

双击和“p”都应该执行相同的命令。使用鼠标时,我可以看到列表框项被选中。我有一种预感,即 MouseBinding Command 属性不是依赖属性,但我不知道如何确认这一点。

采纳答案by Japple

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

还有一些关于为什么 MVVM 中的一点代码隐藏不一定是一件可怕的事情的讨论: 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/

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

回答by MBen

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

ListBox 似乎不处理对 ListBoxItem 的双击。这是一个很好的答案: Can't bind Command to ListBox