2011-02-01 6 views
3

4 개의 열을 포함하는 ListView가 있는데, 동적으로 항목을 추가하고 있습니다.ListView 항목 정렬?

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

이제 동적으로 생성 된 ListView를 perticular 열 클릭으로 정렬하고 싶습니다. 어떻게 이것을 할 수 있습니까?

답변

0

나는 사용자 지정 정렬을 설명하는 문서 here을 발견했습니다.

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.