开发者

Scrolling to the Previously Selected ListBox item

开发者 https://www.devze.com 2023-02-15 10:29 出处:网络
I am having trouble setting the Selected Item of a Listbox I am populating and adding to the LayoutRoot\'s children in code.

I am having trouble setting the Selected Item of a Listbox I am populating and adding to the LayoutRoot's children in code.

I am creating the ListBox over when going back to the page, so I am saving a variable which will tell me what the selected item was before the user clicked.

I tried setting SelectedIndex, but that did not seem to work. That selects the开发者_运维技巧 item and calls SelectionChanged, but the item does not come into focus.

I also tried the combination of MyListBox.ScrollIntoView(MyListBox.Items[MyListBox.SelectedIndex]) and MyListBox.UpdateLayout(), but that did not seem to work either. The item does not seem to come into focus.


Try MyListBox.SelectedItem.EnsureVisible().

(If it doesn't have SelectedItem (ListBox on the desktop CLR doesn't), then use SelectedItems[0] instead)

E: Okay, looks like Windows Phone doesn't support that. However, it does support MyListBox.EnsureVisible(MyListBox.SelectedItems[0])


I ended up fixing this by Adding the UserControl that had the ListBox into the Page's XAML, and the ListBox into the UserControl's XAML.

Then, I was able to use the ScrollIntoView(MyListBox.Items[mySavedSelectedItem]);

I simply saved this value when the user made the selection.

mySavedSelectedItem = ((ListBox)sender).SelectedIndex;
0

精彩评论

暂无评论...
验证码 换一张
取 消