Select item programmatically in WPF ListView

前端 未结 4 1760
[愿得一人]
[愿得一人] 2020-12-03 05:16

I\'m unable to figure out how to select an item programmatically in a ListView.

I\'m attempting to use the listview\'s ItemContainerGenerator, but it just doesn\'t s

4条回答
  •  既然无缘
    2020-12-03 05:48

    Where 'this' is the ListView instance. This will not only change the selection, but also set the focus on the newly selected item.

      private void MoveSelection(int level)
      {
         var newIndex = this.SelectedIndex + level;
         if (newIndex >= 0 && newIndex < this.Items.Count)
         {
            this.SelectedItem = this.Items[newIndex];
            this.UpdateLayout();
            ((ListViewItem)this.ItemContainerGenerator.ContainerFromIndex(newIndex)).Focus();
         }
      }
    

提交回复
热议问题