I have found that there are some strange happenings with the ListView in WPF (as of 8/2007). There seem to be some internal bugs dealing with internal selection properties. I have the ListView selection mode set to single (SelectionMode="Single") and I although I cannot select more than one item using control or shift, when I return from an edit the ListView selects multiple items.
See how the SelectedItems has a count of 2 and SelectionMode is single:
When the code returns to the application, it then proceeds to select multiple items:
This is a strange issue, obviously a ListView bug.
Here is my work around, found by colleage Tom Sprows. Simply set the selected item to multiple, clear the selected items, do the operation and set the selection mode back to single.
When the row is selected and a method that will perform some operation upon the selected item. You must do the following:
1. Get the selected item from the ListView and set it to a local variable.
Product selectedProduct = listView.SelectedItem as Product;
2. Set the Selection Mode to multiple
listView.SelectionMode = SelectionMode.Multiple;
3. Clear the selectedItems using the object.SelectedItems.Clear()
listView.SelectedItems.Clear();
4. Perform whatever operation you are going to perform on the selected item and when it returns, set the Datalist.SelectedItem back to the originally selected item.
PerformOperation(selectedProduct);
listView.SelectedItem = selectedProduct;
5. Set the Selection mode back to single
listView.SelectionMode = SelectionMode.Single;