回车(Enter)的防止默认行为,向上和向下箭头键在XAML / C#一个ListView(窗10) [英] Preventing default behaviour of Return(Enter), Up and Down Arrow keys for a ListView in XAML/C# (Windows 10)

查看:349
本文介绍了回车(Enter)的防止默认行为,向上和向下箭头键在XAML / C#一个ListView(窗10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ListView具有焦点,一个输入按键的默认行为是选择列表视图的第一要素,向上和向下箭头键滚动列表视图。 。我试图阻止这种默认行为,并连接我自定义的逻辑

When a listview has focus, the default behaviour of an enter key press is to pick the first element of the listview, Up and down arrow keys scrolls the listview. I am trying to prevent this default behaviour and hook up my custom logic.

我能够实现使用的KeyDown的列表视图访问键如下:

I am able to implement Access keys using KeyDown for a listview as follows:

后面的代码的方法:

CoreWindow.GetForCurrentThread().KeyDown += KeyDownHandler;



MVVM方式:

MVVM approach:

<ListView SelectedIndex="{Binding IsSelected, Mode=TwoWay}"/>



触发该keydown属性:

Triggering the Keydown property:

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

和使用行为,以滚动列表视图中所选择的指数的滚动条:

And used behaviours to scroll the scrollbar of the listview to the selected index:

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



以上处理越来越触发时,列表视图没有焦点。当列表视图有焦点的默认行为箭头向上,向下和Enter键触发得到,而不是我的附加行为。有没有一种方法,以防止默认行为?

The above handlers are getting triggered when the listview doesn't have focus. When the listview has focus, the default behaviour of arrow up, down and Enter key is getting triggered and not my attached behaviour. Is there a way to prevent the default behaviour?

推荐答案

考虑延长的ListView 控制和重写的onkeydown 处理程序

Consider extending the ListView control and overriding the OnKeyDown handler.

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);
    }
}

这篇关于回车(Enter)的防止默认行为,向上和向下箭头键在XAML / C#一个ListView(窗10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆