Wpf – Programmatically selecting items in a WPF ListView

listviewwpf

How can I set the selected items in a WPF ListView programmatically?

Basically, I have a List<SomeEntity> and a ListView that is bound to another List<SomeEntity>. I need to mark the items that exist on the first list as selected.

Best Answer

var lv = yourListView;
lv.SelectedItems.Clear();
foreach(var item in selection)
     lv.SelectedItems.Add(item);

http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selecteditems.aspx