Select item programmatically in WPF ListView

前端 未结 4 1769
[愿得一人]
[愿得一人] 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:55

    In case you are not working with Bindings, this could also be a solution, just find the items in the source and add them to the SelectedItems property of your listview:

     lstRoomLights.ItemsSource = RoomLights;
     var selectedItems = RoomLights.Where(rl => rl.Name.Contains("foo")).ToList();
     selectedItems.ForEach(i => lstRoomLights.SelectedItems.Add(i));
    

提交回复
热议问题