9

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

在这个 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>

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

4

3 回答 3

12

您的示例中发生的情况是列表框本身对双击做出反应,但仅在列表框项目未覆盖的部分区域中。

您需要将事件处理程序绑定到列表框项。

一些方法在这里: 双击列表框项目以打开浏览器

还有一些关于为什么 MVVM 中的一些代码隐藏不一定是可怕的事情的讨论: 使用 MVVM 从 WPF ListView 项目中触发双击事件

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

于 2012-06-23T20:30:17.793 回答
0

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

于 2012-06-23T20:31:42.440 回答
0

有人可能会争论天气或不代码隐藏是可怕的,但它可能使用命令。像这样将 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>
于 2022-01-02T08:39:20.887 回答