Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.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
防止XAML/C#(Windows 10)中ListView的默认返回(Enter)、上下箭头键行为_C#_Visual Studio_Xaml_Windows 10_Uwp - Fatal编程技术网

防止XAML/C#(Windows 10)中ListView的默认返回(Enter)、上下箭头键行为

防止XAML/C#(Windows 10)中ListView的默认返回(Enter)、上下箭头键行为,c#,visual-studio,xaml,windows-10,uwp,C#,Visual Studio,Xaml,Windows 10,Uwp,当listview具有焦点时,按enter键的默认行为是拾取listview的第一个元素,上下箭头键滚动listview。我试图阻止这种默认行为,并连接我的自定义逻辑 我能够使用KeyDown为listview实现访问键,如下所示: 代码隐藏方法: CoreWindow.GetForCurrentThread().KeyDown += KeyDownHandler; <ListView SelectedIndex="{Binding IsSelected, Mode=TwoWay}"/&

当listview具有焦点时,按enter键的默认行为是拾取listview的第一个元素,上下箭头键滚动listview。我试图阻止这种默认行为,并连接我的自定义逻辑

我能够使用KeyDown为listview实现访问键,如下所示:

代码隐藏方法:

CoreWindow.GetForCurrentThread().KeyDown += KeyDownHandler;
<ListView SelectedIndex="{Binding IsSelected, Mode=TwoWay}"/>
MVVM方法:

CoreWindow.GetForCurrentThread().KeyDown += KeyDownHandler;
<ListView SelectedIndex="{Binding IsSelected, Mode=TwoWay}"/>

触发Keydown属性:

<core:EventTriggerBehavior EventName="KeyDown">
        <core:InvokeCommandAction Command="{x:Bind VMDataContext.KeyDownCommand}" />
    </core:EventTriggerBehavior>

并使用行为将listview的滚动条滚动到所选索引:

<corebehaviors:ListViewScrollBehaviour SelectedIndex="{x:Bind IsSelected, Mode=OneWay}"/>

当listview没有焦点时,会触发上述处理程序。当listview具有焦点时,将触发向上、向下和Enter键的默认行为,而不是我附加的行为。有没有办法防止默认行为?

试试这个

CoreWindow.GetForCurrentThread().KeyDown += new KeyEventHandler(ListView_KeyDown);

private void ListView_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
        //do ur stuff
}

更好的方法是使用
PreviewKeyDown
事件,而不是
KeyDown
所谓的Enter键(单击链接查看MSDN文档)。这应该可以做到:

        private void UIElement_OnKeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {

            }
        }

希望这有帮助。

考虑扩展
列表视图
控件并重写
OnKeyDown
处理程序

public class ExtendedListView : ListView
{
    protected override void OnKeyDown(KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.Enter || e.Key == VirtualKey.Up || e.Key == VirtualKey.Down)
        {
            return;
        }

        base.OnKeyDown(e);
    }
}

ListView_KeyDown(CoreWindow sender,KeyEventArgs args)只能获取CoreWindow和KeyEventArgs的参数。我也无法使用PreviewKeyDown。不确定这是否适用于WinRT应用程序。如前所述,当按下enter键时,不会触发ListView的enter键关闭事件。相反,它正在打开ListView的第一项是否可以覆盖ListView的默认行为以按Enter键?我猜您的意思是用“打开第一项。。。如果在ListView本身上执行此操作,则可以导航到其他页面。但是,您必须考虑到,如果没有预先选择任何项目(例如使用箭头键),它将始终进入第一个项目。如果选择了另一个,则应使用此项。是否有方法防止Enter键出现“默认打开”行为?即使我按下回车键,也不会发生任何事情。然后我可以将我的自定义逻辑附加到它。通常,只需添加代码就可以了。如果没有,ListView XAML上更详细的视图将有助于确定这种行为的原因。谢谢Justin!它适用于回车键,而不适用于上下键。这很奇怪,因为上下箭头键没有命中断点。我还尝试重写OnKeyUp函数并观察到相同的行为。顺便说一句,您可能需要调用
e.Handled=true在某些情况下,而不是
返回