Search code examples
windows-phone-7silverlight-toolkit

Remembering Position of LongListSelector


I'm developing a Windows Phone 7 (WP7) app and I'm using a LongListSelector control for a list. When the user taps on one of the items, my event handler navigates to a new page for the selected item. However, when the user clicks on the back button to return to the previuos page, the LongListSelector is at a different position than it was. Does anyone know how to remember the position of the selector and restore that position when it is returned to?


Solution

  • When you handle the SelectionChanged event, you can save the SelectedItem (which I assume you are already retrieving in order to determine the new page) to a page property. Then in the OnNavigatedTo event for the page, if that item is not null then you can use the ScrollTo Method. Something like this (Where lls is your longlistselector):

    private object previousItem = null;
    private void lls_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        object previousItem = lls.SelectedItem;
        //Page Navigation Magic
    }
    
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
    
        if (previousItem != null)
            lls.ScrollTo(previousItem);
    }