This project has moved and is read-only. For the latest updates, please go here.
1
Vote

SeletedItem of longlist is disabled for selection when navigating back to same page

description

Let us assume you have a longlistselector in Mainpage.xaml. You have a list of Categories displayed in the longlist selector. If I select the category "Days", I am navigating to a items page where Days of the week are displayed. Now I hit the hardware back button and come back to the MainPage.xaml. When I try to select the same category "Days" again, nothing happens. I am not able to select the Category "Days".

I am forced to select some other category and navigate once before I can again select the category "Days".

This is easily replicable in the sample application provided with the toolkit.

Select the the item "Alex Hoover" under buddies. It takes you to status of "Alex Hoover". then hit the hardware back button. After you comeback to buddies page, you can no more select "Alex Hoover".
Now you are forced to select someone else and navigate back once before you can select "Alex Hoover" again.

I am badly in need for a fix. Please let me know a solution for the same.

I am using MVVM pattern and binding items to Page DataContext and ItemSource of longlistselector, with twoway binding within the datatemplate for longlistselector. I am binding the SelectedItem property of longlistselector to a property in my viewmodel.

comments

richardwoo wrote Jan 15, 2014 at 9:09 AM

This is normal and expected behavior in a ListBox or LongListSelector, but the code sample didn't implement a good SelectionChanged event handler. Note that the event is called Selection Changed. When you first click on an item, it becomes selected and the event fires. If you click on the same item again, the item is still selected and the selection hasn't changed and the event won't fire again. The usual way to deal with this is to reset the SelectedItem (make no items be selected) before exiting the event handler. Then clicking any item will always cause the event to fire.

For a code example, open a new Windows Phone project and select the Windows Phone Databound App template. Examine the code in the SelectionChanged event handler in MainPage.xaml.cs. This is very typical code for a SelectionChanged handler.

To fix the Toolkit sample code, use this for the SelectionChanged event handler in LongListSelectorSample.xaml.cs (I added only one line, to set SelectedItem to null.
private void PersonSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Person person = buddies.SelectedItem as Person;
    if (person != null)
    {
        NavigationService.Navigate(new Uri("/Samples/PersonDetail.xaml?ID=" + person.ID, UriKind.Relative));

        buddies.SelectedItem = null;  // reset SelectedItem to no selection
    }
}   
}