#992 – Scrolling an Item in a ListBox into View

You can programmatically scroll an item in a ListBox into view by using the ListBox.ScrollIntoView method.

In the example below, we scroll the actor Jane Wyman into view when the user clicks the button.

        <ListBox Name="lbActors" Margin="15,5" Width="200" Height="150"
                 ItemsSource="{Binding ActorList}"
                 DisplayMemberPath="NameAndDates"/>
        <Button Content="Find Jane Wyman" Margin="10"
                Click="btnFindJane_Click"/>

992-001

In the Click event handler for the button, we use Linq to find the Actor object for Jane Wyman and we then call ScrollIntoView.

        private void btnFindJane_Click(object sender, RoutedEventArgs e)
        {
            Actor jane = (from a in ActorList
                         where a.FirstName == "Jane"
                            && a.LastName == "Wyman"
                         select a).First();
            lbActors.ScrollIntoView(jane);
            lbActors.SelectedItem = jane;
        }

992-002

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #992 – Scrolling an Item in a ListBox into View

  1. Pingback: Dew Drop – January 22, 2014 (#1707) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: