Arrow keys don't work after programmatically setting ListView.SelectedItem

前端 未结 9 1765
感情败类
感情败类 2020-12-16 02:34

I have a WPF ListView control, ItemsSource is set to an ICollectionView created this way:

var collectionView = 
  System.Windows.Data.CollectionViewSource.Ge         


        
9条回答
  •  我在风中等你
    2020-12-16 02:56

    Cheeso's solution DOES work for me. Prevent the null exception by just setting a timer.tick to do this, so you have left your original routine.

    var uiel = (UIElement)this.lv1.ItemContainerGenerator                        
               .ContainerFromItem(lv1.Items[ix]); 
    if (uiel != null) uiel.Focus();
    

    Problem solved when calling timer after a RemoveAt/Insert, and also at Window.Loaded to set focus and select to first item.

    Wanted to give back this first post for the much inspiration and solutions I got at SE. Happy coding!

提交回复
热议问题