WPF Listbox displays next item after SelectedItem

I have a TextBox and a ListBox with a bunch of items.

The TextBox has a KeyDown event handler, the idea is to allow the user to press the up and down keys to scroll within the ListBox while the focus is on the TextBox.

When the user presses the down key multiple times, the selected item becomes the last visible item on the screen. If the user has reached the bottom of a visible list item on the screen, I want them to see the next item after the selected item.

+1


a source to share


1 answer


Take a look at the ScrollIntoView method in the list. You can use this to always see the next item for the selected one.

Click the down arrow:

if (listbox.SelectedIndex < listbox.Items.Count - 1)
    listbox.ScrollIntoView(listbox.Items[listbox.SelectedIndex + 1]);

      



Press the up arrow:

if (listbox.SelectedIndex > 0)
    listbox.ScrollIntoView(listbox.Items[listbox.SelectedIndex - 1]);

      

+7


a source







All Articles