简体   繁体   中英

WPF Listbox display next element after SelectedItem

I have TextBox and ListBox with bunch of elements.

TextBox has KeyDown event handler, the idea behind this is to allow user to press up and down keys to scroll inside ListBox while focus is on TextBox.

When user presses "down key" several times, selected element becomes last visible element on screen. If user has reached bottom of visible list element on screen, I want him to see next element after selected element as well.

Look at the ScrollIntoView method on the listbox. You can use this to ensure that the next element to the selected one is always visible.

On down arrow press:

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

On up arrow press:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM