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! ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With