2
votes

i have a ListBox that i bind data to. however, after i bind data to the ListBox, the scroll position is all the way at the bottom. i need to get the scroll position to go to the top.

i searched on the internet and one approach was as follows.

listBox.ScrollIntoView(lastItemIndex);
listBox.UpdateLayout();
listBox.ScrollIntoView(firstItemIndex);

this approach works as intended, however, when i bind even as few as 20 to 30 items, the listBox.UpdateLayout() call takes on the order of +4 seconds on my development phone (1 GHz Qualcomm, 8 GB internal memory).

is there anyway to get the effect i want (programmatically scroll to the top) without such an expensive operation? can't i specify how to at bind time to scroll to the top?

thanks for any help.

3
Just to confirm, you want the list to have a 'scrolling to top automatically when page is navigated to' effect? (I.e. not just have the list be at the top from the beginning) - William Melani

3 Answers

3
votes
listbox.ScrollIntoView(listbox.Items.First());
2
votes

Answering the above follow-up question about accessing the internal scrollviewer - see this answer:

horizontal offset for scrollviewer in gridview C#

1
votes

You'll need to call ScrollToVerticalOffset on the ListBox's internal ScrollViewer.