Сортировка элементов ListView? - PullRequest
3 голосов
/ 01 февраля 2011

У меня есть ListView, который содержит четыре столбца, в которые я добавляю элементы динамически, как:

ListViewItem lvi = new ListViewItem();
lvi.Background = ... color you want ... ;
lvi.Content = new {Server = "test1", .... };
listViewResult.Items.Add(lvi);

Теперь я хочу отсортировать этот динамически сгенерированный ListView по щелчку столбца. Как мне этого добиться?

1 Ответ

0 голосов
/ 16 октября 2015

Я нашел статью здесь , которая объясняет пользовательскую сортировку.

VirtualizingStackPanel.IsVirtualizing=”True”

First you need to specify the above property to true in your ListView, (this is the default value for ListView in WPF).

Then next, you need to use custom sorter, instead of SortDescriptions as described in my earlier blog. The key is using the CustomSort property of ListCollectionView:
ListCollectionView view = (ListCollectionView)CollectionViewSource.GetDefaultView(myListView.ItemsSource);

Then in your ColumnHeader click event handler, you add something like the following:

view.CustomSort = sorter;
myListView.Items.Refresh();

Where sorter is a custom class you implement the IComparer interface.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...