I’ve created a ListView in my XAML file and set the SelectionMode to “Extended”. I’d like to write a method that manually sets the selection on this list view.
The following works set set the selection for one item.
MyListView->SelectedItem = SomeObject;
SelectedIndex works just as well.
How do I set the selection for multiple items?
Make sure your ListView’s
SelectionModeis “Multiple”, and then you can just append to the SelectedItems collection on ListView.This thread also includes a WPF example where you can use databinding/MVVM to accomplish the same goals (assuming your individual ListViewItems are bindable): http://social.msdn.microsoft.com/Forums/en/wpf/thread/2418b2a1-6fbd-4820-b18f-e693111debbb
Note that the SelectedItems collection compares to the ItemsSource collection typically by object instance identity. So, for example, this code will not cause the items to be selected:
But changing the last two lines to this, will cause the items to become selected: