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!

msfriese
  • 111
  • 3
  • 9
  • possible duplicate of http://stackoverflow.com/questions/9689987/listbox-shift-click-multi-select-anchor-is-not-being-set-properly – dnr3 Sep 03 '13 at 09:26
  • 1
    try set the focus to your `selectedItem` within the `ListSelectionChanged` event handler. I think keyboard navigation is about focus not selection. – Bolu Sep 03 '13 at 09:31

1 Answers1

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();
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 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. – msfriese Sep 03 '13 at 12:38
  • For future reference, [here's](http://stackoverflow.com/a/10463162/1229237) another method. The answer to that question is the complete code for this question. – DJH Oct 01 '13 at 07:37