KeyDown event is not working for Arrow keys, Home, End, PageUp, PageDown and other similar keys in silverlight

。_饼干妹妹 提交于 2019-12-08 07:32:54

问题


I want to override the selection behavior of ListBox Items.

We can traverse through ListBox items using Up and Down arrows but I want to traverse the list using Left and Right arrow keys.

While I am trying to add the key down event for ListBox, It shows almost all key presses except for Arrow Keys, Home, End and similar keys.

I have the following code:

private void listArtist_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key.ToString() == "Enter")
    {
        // Go to some page
    }
    else
    {
        MessageBox.Show(e.Key.ToString());
    }
}

I am clueless about this. Please help.

Thanks, Subhen


回答1:


What you need to do is subclass ListBox and override the OnKeyDown function. Maybe something like this:

class MyListBox : ListBox
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        switch (e.Key)
        {
        case Key.Left:
            // ....
            e.Handled = true;
            break;
        case Key.Right:
            // ....
            e.Handled = true;
            break;
        }

        if (!e.Handled)
            base.OnKeyDown(e);
    }
}



回答2:


Do you mind if i give the idea of handling keyup event instead

http://weblogs.asp.net/jgalloway/archive/2007/07/02/some-keyboard-input-tricks-for-silverlight-1-1-alpha.aspx



来源:https://stackoverflow.com/questions/2521362/keydown-event-is-not-working-for-arrow-keys-home-end-pageup-pagedown-and-oth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!