Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将按键事件绑定到WPF中的ListViewItem_C#_Wpf_Mvvm - Fatal编程技术网

C# 将按键事件绑定到WPF中的ListViewItem

C# 将按键事件绑定到WPF中的ListViewItem,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个ListView,它用包含图像和文本的ListViewItems(文件浏览器)填充视图。在遵守MVVM设计模式的情况下,当用户按下选定项上的“Enter”键时,如何启动命令?我已经寻找并找到了一些解决方案,但似乎没有一个适合我 <ListView ScrollViewer.HorizontalScrollBarVisibility="Hidden" VirtualizingPanel.IsVirtualizing="True" Virtual

我有一个ListView,它用包含图像和文本的ListViewItems(文件浏览器)填充视图。在遵守MVVM设计模式的情况下,当用户按下选定项上的“Enter”键时,如何启动命令?我已经寻找并找到了一些解决方案,但似乎没有一个适合我

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


鼠标夹很好用。我尝试将KeyBinding放在ListView而不是网格中,并使用SelectedItem属性获取聚焦项,但仍然没有结果

ItemTemplate
中的根
Grid
或视图代码隐藏中的
ListViewItem
容器执行
PreviewKeyDown
事件,然后从那里简单地执行命令,例如:

private void ListViewItem_PreviewKeyDown(object sender, KeyEventArgs e)
{
    var viewModel = DataContext as YourViewModel;
    viewModel.YourCommand.Execute(null);
}


或者实现一种行为,该行为钩住事件处理程序并执行相同的操作:

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

MVVM不是要从视图中删除代码,而是要分离关注点。如果使用
KeyBinding
或事件处理程序调用该命令,则无所谓。

请尝试
手势=“Enter”



为什么您找到的解决方案不适合您?你的特殊和孤立的问题是什么?有例外吗?意外行为?问题是当我按下“回车”键时,什么也没有发生。未调用处理程序函数。已尝试,但未发生任何事件。。。从我所读到的,它与键盘没有集中在视图的项目上有关。可能还有其他控件“吞咽”了某些组合键。按照我的建议处理PreviewKeyDown事件,你应该会没事的。谢谢,调查一下。但是到目前为止,如果我为网格实现该事件,它是不起作用的。它根本没有被调用,可能是因为我实际上是在单击图像或文本块吗?他们会在网格的顶端吗?我正在寻找一个解决方案,网格需要集中精力。您也可以处理ListView本身的PreviewKeyDown事件。如果我从ListView处理它,则会出现内部错误:Debugger::HandleIPCEvent中未处理的异常,我无法尝试捕获此错误。我想我必须以某种方式从ListViewItem处理它。嗯……这似乎是调试器错误。如果在没有调试器的情况下运行,则不应出现异常。有关如何连接ListViewItem的事件处理程序的示例,请参阅我编辑的答案。
<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <EventSetter Event="PreviewKeyDown" Handler="ListViewItem_PreviewKeyDown" />
    </Style>
</ListView.ItemContainerStyle>
<Grid.InputBindings>
    <KeyBinding Gesture="Enter" Command="{Binding EnterCommand}" />
</Grid.InputBindings>