2

I am working on a WPF ListView and I want to use the keyboard navigation which actually works fine right now. The problem is as follows:

  1. I listen to SelectionChanged on the ListBox
  2. inside the event handler I bring the selected Item into view (which works fine)
  3. when I start Keyboard navigation it starts from the top of the list, not from the SelectedItem (which is the thing i don't want).

So the question is now: how can I start keyboard navigation (up and down arrows) from the SelectedItem instead of the first Item?

Here ist what my event handler looks like:

protected void ListSelectionChanged ( Object sender , SelectionChangedEventArgs args ) { var enumerator = args.AddedItems.GetEnumerator( ); if ( enumerator.MoveNext( ) ) ( sender as ListView ).ScrollIntoView( enumerator.Current ); } 

Thx in advance!

2
  • possible duplicate of stackoverflow.com/questions/9689987/… Commented Sep 3, 2013 at 9:26
  • 1
    try set the focus to your selectedItem within the ListSelectionChanged event handler. I think keyboard navigation is about focus not selection. Commented Sep 3, 2013 at 9:31

1 Answer 1

4

I think that @Bolu has correctly answered your question. The problem relates to the Focus of the item, not the selection. When you change the SelectedItem, try adding this line just afterwards:

item.Focus(); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, calling Focus() was the right thing to do. Anyways I had some problems getting the Item to call Focus on. The first reason was: It was not there yet. The second one was: It was out of sight so the Virtualization did not create it. To solve this I had to ScrollIntoView(value) first and then listen to ItemsContainerGenerator.StatusChanged to have the item created. Finally i could savely call focus and everything worked out fine.
For future reference, here's another method. The answer to that question is the complete code for this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.