Pat Long - Munkii Yebee
Pat Long - Munkii Yebee

Reputation: 3629

Resetting SelectedItem on a Listbox bound to a RelayCommand on a ViewModel

Using the EventToCommand behaviour that comes with MVVM Light I am binding the SelectedItem of a ListBox to a RelayCommand on a ViewModel

All works great in my Windows Phone 7 app except that after navigating away from the View with the ListBox then back the SelectedItem is the same as before. Not what I want.

I tried reseting the selected index when navigating away but that causes the Command to trigger again.

Has anyone else solved this issue and how?

TIA

Patrick Long

Upvotes: 1

Views: 352

Answers (3)

Pat Long - Munkii Yebee
Pat Long - Munkii Yebee

Reputation: 3629

I switched away from using the SelectionChanged event. Now I do it all with TapGestures. Downside of this is that TapGesture does not work with EventToCommand behaviour so I am trapping the Gesture event in the View and firing the Command that is bound to the DataContext of the Sender.

IMHO this is a lot neater than using SelctionChanged and mucking about with SelectedIndexes.

Upvotes: 0

Matt Lacey
Matt Lacey

Reputation: 65556

I handle this by a simple check at the start of each handler. Assuming that you are only supporting the single selection of items you can check if there is an added item.

if (e.AddedItems.Count == 1)
{
    // Your code here
}

(Where e is an instance of SelectionChangedEventArgs.)

If the selection has been removed the AddedItems list will be empty but the RemovedItems list will be populated instead.

Upvotes: 1

Derek Beattie
Derek Beattie

Reputation: 9478

Since you're using mvvm light can't you call the clear method in the ViewModelLocator for the specific view model?

Upvotes: 0

Related Questions