Bish25
Bish25

Reputation: 616

Stop listbox from navigating with arrows C# WPF

I have a canvas which elements can be moved with the arrow keys, however when i select an element in a listbox that is currently on the canvas. The arrow key down will filter down the list until it reaches the bottom then will move the element on the canvas. the other problem is it will then move the element at the very bottom of the listbox that is now selected.

Upvotes: 0

Views: 373

Answers (1)

mm8
mm8

Reputation: 169360

You could handle the PreviewKeyDown event for the ListBox:

<ListBox x:Name="lb" PreviewKeyDown="lb_PreviewKeyDown">
    <ListBoxItem>1</ListBoxItem>
    <ListBoxItem>2</ListBoxItem>
    <ListBoxItem>3</ListBoxItem>
</ListBox>

private void lb_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Down || e.Key == Key.Up)
        e.Handled = true;
}

Upvotes: 1

Related Questions