wp7appdevelopment

Adventures in Windows Phone app development

Scrolling to the Selected Item Listbox

If you’re developing an Windows Phone app, you will often use a ListBox to display information. Here’s how to set the selected item programmatically and scroll the selected item into view. This example assumes you’re working with C# and Silverlight.

Selecting an item in a listbox

You can select a specific item in a ListBox using the SelectedIndex property. For example, to select the sixth item (index=5) in a list, use the following in your C# code:

  myList.SelectedIndex = 5;

Scrolling Selected Item into View

The ListBox.ScrollIntoView method can be used to scroll to a particular place in a ListBox. For example, to scroll down your ListBox items so that the selected item is displayed, use the following in your C# code:

   myList.ScrollIntoView(myList.SelectedItem);

One note — if you’ve changed the contents of your ListBox (i.e., if the contents of your ItemsSource collection changes), you may need to call UpdateLayout() before calling ScrollIntoView():

   UpdateLayout();
   myList.ScrollIntoView(myList.SelectedItem);

References

ListBox.ScrollIntoView Method

ListBox Class

Leave a comment