【问题标题】:WPF Listbox display next element after SelectedItemWPF Listbox 在 SelectedItem 之后显示下一个元素
【发布时间】:2009-05-12 09:21:30
【问题描述】:
我有带有一堆元素的 TextBox 和 ListBox。
TextBox 具有 KeyDown 事件处理程序,其背后的想法是允许用户在焦点位于 TextBox 时按上下键在 ListBox 内滚动。
当用户多次按下“向下键”时,所选元素将成为屏幕上最后一个可见元素。如果用户已到达屏幕上可见列表元素的底部,我希望他也能看到所选元素之后的下一个元素。
【问题讨论】:
标签:
c#
.net
wpf
listbox
listboxitem
【解决方案1】:
查看列表框上的ScrollIntoView 方法。您可以使用它来确保所选元素的下一个元素始终可见。
按下向下箭头:
if (listbox.SelectedIndex < listbox.Items.Count - 1)
listbox.ScrollIntoView(listbox.Items[listbox.SelectedIndex + 1]);
按向上箭头:
if (listbox.SelectedIndex > 0)
listbox.ScrollIntoView(listbox.Items[listbox.SelectedIndex - 1]);