ravedome
ravedome

Reputation: 163

WPF: listview display recently added items on top instead of bottom

I got a collection bind to a listview. This collection gets items added every 4-5 seconds and new items will automatically be added in bottom of the listview. So if you gonna see newest items then you need to scroll down to the bottom.

My question is: is it possible to like reverse the listview so the new newest items are on top and oldest items in bottom ?

Thanks

Upvotes: 16

Views: 11593

Answers (4)

josepainumkal
josepainumkal

Reputation: 1733

Two approaches:

1) Use CollectionChange event to capture any item-change in the list. Then sort the list in descending order and then render.

// subscribe to CollectionChanged event of the ObservableCollection
_recordingsHidden.CollectionChanged += RecordingsHiddenOnCollectionChanged;
private void RecordingsHiddenOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
        var view = (CollectionView)CollectionViewSource.GetDefaultView(lvCallHistory.ItemsSource);
        view.SortDescriptions.Add(new SortDescription("StartTime", ListSortDirection.Descending));
}

Here 'lvCallHistory' is my list view and sorting is done based on 'StartTime'. This approach is effective only if the list contains less items. Because sorting is a costly operation and if you update the list frequently, then it might affect the performance of the application. The user may experience 2-3 seconds lag each time the view is rendered.

2) Avoid sorting: Every time you update the list, point the scroll bar to the last added item. (Pros: Very effective and no performance issue. Cons: Items still added to the end of list, even though scroll bar takes you to the new item whenever a new entry comes.)

lvCallHistory.SelectedItem = <your_collection>.Last();
lvCallHistory.ScrollIntoView(lvCallHistory.SelectedItem);

Upvotes: 0

Northstar
Northstar

Reputation: 343

Set sorting order to descending and use add items

this.listView1.Sorting = System.Windows.Forms.SortOrder.Descending;

listView1.Items.Add( listviewitem);

or remove sorting and insert item

listView1.Items.Insert(0, listviewitem);

either of them will work..

Upvotes: -1

Mark A. Donohoe
Mark A. Donohoe

Reputation: 30368

Even though you already voted the other answer as accepted, as they pointed out, while that is a simple, one-line solution, that does a lot of shifting and such which can affect performance.

IMHO, a better, correct way to do this would be to use sorting. It's easy to get the default view of a collection, then apply sorting to it, or just use a CollectionViewSource and apply sorting there. Of course that means you have to have a field to sort on, but that could be as simple as adding an index property if you don't already have a time-stamp or something similar.

Let me know if you want a code example here and I'll provide one.

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292425

Use Insert instead of Add :

collection.Insert(0, newItem);

Note that it's slower than Add since it has to shift all items by 1 position. It might be an issue if the list is very big.

Upvotes: 30

Related Questions