Q:

How to Scroll To End of ListView After Data Added - WPF

I tried adding the following in a button click handler:
        ListView listView = MyListView;
        int lastItemIndex = listView.Items.Count - 1;
        listView.ScrollIntoView(listView.Items[lastItemIndex]);
        listView.UpdateLayout();

The button click is also associated with a command handler that adds an item to the ObservableCollection associated with the ListView.
The problem is that the button click handler is called before my command handler so it is too early. In the button click handler, the ListView does not yet see the updated ObservableCollection with the added item. What event or better yet, what can I do without changing the code behind to get the ListView to scroll to the end after my item is added to the ObservableCollection? I have looked but nothing yet in stackoverflow for answers. Thanks!

A:

If you ItemSource is ObservableCollection, you can hook to CollectionChanged event in your Window/UserControl constructor and scroll last item into view whenever item gets added in a collection.
Assuming your underlying class is TesClass, this is how you will do it:
((INotifyCollectionChanged)listView.ItemsSource).CollectionChanged +=
     (s, e) =>
     {
         if (e.Action == 
             System.Collections.Specialized.NotifyCollectionChangedAction.Add)
         {
             listView.ScrollIntoView(listView.Items[listView.Items.Count - 1]);
         }
     };

