将keypress事件绑定到WPF中的ListViewItem

时间:2017-09-22 13:25:40

标签: c# wpf mvvm

我有一个ListView,它使用包含图像和文本(文件浏览器)的ListViewItems填充视图。当用户按下“输入”键时,如何触发命令?在尊重MVVM设计模式的同时键入所选项目?我已经搜索并找到了一些解决方案,但它们似乎都不适合我。

<ListView ScrollViewer.HorizontalScrollBarVisibility="Hidden"
          VirtualizingPanel.IsVirtualizing="True"
          VirtualizingPanel.ScrollUnit="Item"
          Background="#fdfaf4"
          Name="filesView"
          ItemsSource="{Binding Items}">

    <ListView.ItemTemplate>
        <DataTemplate>

            <!-- The image and item name -->
            <Grid Width="{Binding ActualWidth, ElementName=filesView, Converter={x:Static converter:GridWidthToListViewWidthConverter.Instance}}" 
                  Background="Transparent">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.07*" MinWidth="25" MaxWidth="40" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <!-- Drive, file or folder -->
                <Image Grid.Column="0"
                       Margin="0,0,5,0"
                       Name="itemType"
                       Source="{Binding Type,
                       Converter={x:Static converter:HeaderToImageConverter.Instance}}" />

                <!-- The text is binded to the image size, so they'll expand/shrink together -->
                <TextBlock Grid.Column="1"
                           VerticalAlignment="Center"
                           FontSize="{Binding ActualHeight,
                           ElementName=itemType, Converter={x:Static converter:ImageSizeToFontSizeConverter.Instance}}"
                           Text="{Binding Name}" />

                <!-- The command to enter a drive/folder is called from here -->
                <Grid.InputBindings>
                    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding EnterCommand, Mode=TwoWay}" />
                    <KeyBinding Key="Enter" Command="{Binding EnterCommand, Mode=TwoWay}" />
                </Grid.InputBindings>

            </Grid>

        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>

MouseBinding工作得很好。我已经尝试将KeyBinding放在ListView而不是网格中,并使用SelectedItem属性获取焦点项,但仍然没有。

2 个答案:

答案 0 :(得分:2)

在视图代码隐藏中的PreviewKeyDownGrid容器中为根ItemTemplate实现ListViewItem事件,然后从那里执行命令,例如:

private void ListViewItem_PreviewKeyDown(object sender, KeyEventArgs e)
{
    var viewModel = DataContext as YourViewModel;
    viewModel.YourCommand.Execute(null);
}
<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <EventSetter Event="PreviewKeyDown" Handler="ListViewItem_PreviewKeyDown" />
    </Style>
</ListView.ItemContainerStyle>

或者实现一个挂钩事件处理程序并执行相同操作的行为:https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF

这两种方法都没有破坏MVVM模式,因为您从XAML标记所属的完全相同的视图中调用完全相同的视图模型命令。

MVVM 关于从视图中删除代码,它是关于关注点的分离。如果使用KeyBinding或事件处理程序调用命令并不重要。

答案 1 :(得分:0)

尝试Gesture="Enter";

<Grid.InputBindings>
    <KeyBinding Gesture="Enter" Command="{Binding EnterCommand}" />
</Grid.InputBindings>