Kiang Teng
Kiang Teng

Reputation: 1705

Disabling keyboard navigation in WPF ListBox

How do I disable keyboard navigation in a WPF ListBox without disabling selection using the mouse?

Upvotes: 2

Views: 5237

Answers (1)

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104781

Handle the PreviewKeyDown event to and set e.Handled to true (you could check and disable only certain keys/modifiers which are provided with the KeyEventArgs passed to the handler):

XAML:

<ListBox PreviewKeyDown="listBox_PreviewKeyDown">
  <ListBoxItem Content="asdfasdf" />
  <ListBoxItem Content="asdfasdf" />
  <ListBoxItem Content="asdfasdf" />
</ListBox>

Code behind:

private void listBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
  e.Handled = true;
}

Upvotes: 8

Related Questions