Jane Wayne
Jane Wayne

Reputation: 535

how to scroll to the top of a ListBox in windows phone 7?

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.

Upvotes: 2

Views: 2446

Answers (3)

Teoman shipahi
Teoman shipahi

Reputation: 23052

listbox.ScrollIntoView(listbox.Items.First());

Upvotes: 3

BigScary
BigScary

Reputation: 560

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

horizontal offset for scrollviewer in gridview C#

Upvotes: 2

Matt Lacey
Matt Lacey

Reputation: 65556

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

Upvotes: 1

Related Questions