简体   繁体   中英

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
        }
    }

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?

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! ;-)

ScrollIntoView definitely works. I just added an application button to an empty databound app and doing the following on button click caused the list to scroll.

MainListBox.ScrollIntoView(MainListBox.Items.Last());

Could be an issue with an event on selectionChanged ? Do you have anything hooked up to that?
Does the ScrollIntoView work if you don't set the selected item?

you can insert the new item always on the top by

yourItemList.Insert(0, item);

thus no need for ScrollIntoView. May be this will help!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM