I am setting up tombstoning for a simple WP7 app. I have a list of items, and I want to save the ListBox.SelectedIndex in State memory, and on returning to the page, have that item selected in the list.

When I try the following code, saving the value seems to work (I have confirmed by displaying it in a MessageBox) but the list item is not selected.

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {

            if (State.ContainsKey("activeResult"))
            {
                listBox1.SelectedIndex = Convert.ToInt32(State["activeResult"]);
            }
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            State["activeResult"] = listBox1.SelectedIndex;
            base.OnNavigatedFrom(e);
        }

The code compiles with no errors - but the listbox item is just never selected. thanks for your help! cheers Will

有帮助吗?

解决方案

Wild guess says you're databinding the List after you set the SelectedIndex, and as such it have no effect (unless it's zero).

Solution: Ensure your ViewModel is initialized, and loaded before you set the SelectedIndex, or databind the SelectedIndex property, and set it on your ViewModel, rather than on the UI component.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top