I have a WPF ListView control, ItemsSource is set to an ICollectionView created this way:
var collectionView =
System.Windows.Data.CollectionViewSource.Ge
I found a somewhat different approach. I'm using databinding to make sure the correct item is highlighted in the code, and then instead of setting focus on every rebind, I simply add a pre-event handler to the code behind for keyboard navigation. Like this.
public MainWindow()
{
...
this.ListView.PreviewKeyDown += this.ListView_PreviewKeyDown;
}
private void ListView_PreviewKeyDown(object sender, KeyEventArgs e)
{
UIElement selectedElement = (UIElement)this.ListView.ItemContainerGenerator.ContainerFromItem(this.ListView.SelectedItem);
if (selectedElement != null)
{
selectedElement.Focus();
}
e.Handled = false;
}
This simply makes sure that the correct focus is set before letting WPF handle the keypress