Reputation: 4801
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
}
}
Upvotes: 11
Views: 11700
Reputation: 9
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!!
Upvotes: -2
Reputation: 1532
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! ;-)
Upvotes: 5
Reputation: 1090
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?
Upvotes: 35
Reputation: 65564
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?
Upvotes: 3