在WPF ListBox中禁用键盘导航

Kia*_*eng 1 .net c# keyboard wpf listbox

如何在不使用鼠标禁用选择的情况下禁用WPF ListBox中的键盘导航?

Shi*_*mmy 6

处理PreviewKeyDown事件并设置e.Handled为true(您可以仅检查和禁用传递给处理程序的KeyEventArgs提供的某些键/修饰符):

XAML:

<ListBox PreviewKeyDown="listBox_PreviewKeyDown">
  <ListBoxItem Content="asdfasdf" />
  <ListBoxItem Content="asdfasdf" />
  <ListBoxItem Content="asdfasdf" />
</ListBox>
Run Code Online (Sandbox Code Playgroud)

代码背后:

private void listBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
  e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)