Matt Ellen
Matt Ellen

Reputation: 11612

Scroll to bottom on new item. My method freezes scrollbar

I have a ListView that gets updated frequently, at irregular intervals.

I want it so that when an update is received the item will be scrolled into view.

I know the code to do that:

logListView.ScrollIntoView(logListView.Items[logListView.Items.Count - 1]);

I don't know where to put it so that it happens when the ListView is updated.

I've tried putting it in the LayoutUpdated event handler of the ListView, but when I try to scroll during a period when there are no updates (I know there aren't any updates because I've turned the message pump off) the view is stuck at the bottom. I'm guessing this is because scrolling raises a LayoutUpdated event.

How do I get the ListView to scroll to the bottom when it receives a new item?

Upvotes: 0

Views: 1785

Answers (1)

Quartermeister
Quartermeister

Reputation: 59149

Take a look at the answer to WPF ListBox Scroll when item added. The Items property is an ItemCollection, which implements INotifyCollectionChanged, so you can use the CollectionChanged event. It should work for a ListView as well as a ListBox, but if you're using ScrollIntoView then I suspect you have a ListBox anyway.

INotifyCollectionChanged collection = logListView.Items;
collection.CollectionChanged += collection_CollectionChanged;

Upvotes: 2

Related Questions