Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListBox.ScrollIntoView() does not seem to work in WP7

I want to move the listbox scrollbar to the bottom whenever a new item is added to the itemssource, but ScrollIntoView() doesn't seem to do anything if I pass it either a reference to the newly added item, or the index of it. Has anyone gotten this to work, or have any other suggestions as to how I could scroll the listbox down to the bottom?

Some code:

    void Actions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        //if a new item was added, set it to the selected item
        if (e.NewItems != null)
        {
            MainListBox.SelectedIndex = e.NewStartingIndex;

            //MainListBox.ScrollIntoView(MainListBox.Items.Last());     //todo: this doesnt seem to work
        }
    }
like image 728
Henry C Avatar asked Oct 11 '10 09:10

Henry C


2 Answers

MSDN says:

When the contents of the ItemsSource collection changes, particularly if many items are added to or removed from the collection, you may need to call UpdateLayout() prior to calling ScrollIntoView for the specified item to scroll into the viewport.

Could that be your problem?

like image 184
Jac Avatar answered Oct 22 '22 04:10

Jac


THIS is the answer:

http://dotnet-experience.blogspot.com.es/2010/12/wpf-listview-scrollintoview.html

In a few words: the items are loaded into the ListBox asynchronously, so if you call ScrollIntoView() within the CollectionChanged event (or similar) it will not have any items yet, so no scrolling.

Hope it helps, it surely helped me! ;-)

like image 5
Hannish Avatar answered Oct 22 '22 04:10

Hannish