Windows Store App ListBox的PreviewKeyDown

时间:2013-05-20 14:21:41

标签: c# windows-8 listbox windows-store-apps winrt-xaml

对于 Windows应用商店应用,是否等同于PreviewKeyDown?它不可用。

我遇到与described here:

完全相同的问题
  

我有一个ListBox,上面有一个TextBox。我想使用箭头键从ListBox导航到TextBox。目的是如果选择ListBox中的第一个项目,并且用户键入,则TextBox将获得焦点。

1 个答案:

答案 0 :(得分:8)

啊,太棘手了。处理关键事件并不是非常明显。这就是你想要的:

public MainPage()
{
    this.InitializeComponent();
    Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += (s, args) =>
    {
        if ((args.EventType == CoreAcceleratorKeyEventType.SystemKeyDown 
            || args.EventType == CoreAcceleratorKeyEventType.KeyDown)
            && (args.VirtualKey == VirtualKey.Up))
        {
            MoveUp();
        }
        else if ((args.EventType == CoreAcceleratorKeyEventType.SystemKeyDown 
            || args.EventType == CoreAcceleratorKeyEventType.KeyDown)
            && (args.VirtualKey == VirtualKey.Down))
        {
            MoveDown();
        }
    };
}

private void MoveUp()
{
    // this part is up to you
    throw new NotImplementedException();
}

private void MoveDown()
{
    // this part is up to you
    throw new NotImplementedException();
}

祝你好运!