In my application, I have a ListBox
with items. The application is written in WPF.
How can I scroll automatically to the last added item? I want the ScrollViewer
to be moved to the end of the list when new item has been added.
Is there any event like ItemsChanged
?
(I don't want to use the SelectionChanged
event)
Keep in mind that
listBox.ScrollIntoView(listBox.Items[listBox.Items.Count - 1]);
works only if you have no duplicate items. If you have items with the same contents it scrolls down to the first find.Here is the solution I found:
A slightly different approach to those presented so far.
You could use the
ScrollViewer
ScrollChanged
event and watch for the content of theScrollViewer
getting larger.This avoids some issues with the binding to the
ListBox
ItemsSource
changing.The
ScrollViewer
can also be found without making the assumption that theListBox
is using the default control template.Then attach this to the
ListBox
Loaded
event:This could be easily modified to be an attached property, to make it more general purpose.