Alex
Alex

Reputation: 294

WPF popup listbox use keyboard for navigation

Can anyone suggest the solution how can I make navigation using down and up keys in listbox which come on popup. Solutions like set selected items on keyup and keydown event are not working for me. Should I make something more special then just set selected item in this case?

Upvotes: 0

Views: 1092

Answers (1)

Omri Btian
Omri Btian

Reputation: 6547

ListBox already implements selection navigation using keyboard when it is focused.

All you have to do is give it focus when you want, for example in the window that contains it:

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Down)
    {
        listbox.SelectedIndex = 0;
        listbox.Focus();
    }
} 

Because listbox.Focus(); will only give it focus but won't yet change the selection item (which will make the user hit's the "Down" button twice in order to do so) set the ListBox's SelectedIndex first.

Hope this helps

Upvotes: 1

Related Questions