2014-07-26 2 views
0

내 Windows Phone 앱에서 CollectionViewSource.SortDescriptors을 사용하여 LongListSelector의 콘텐츠를 정렬했습니다. 이제 Windows 런타임 앱으로 마이그레이션하여 ListView을 사용하여 콘텐츠를 표시합니다. (그리고 WinRT는 SortDescriptors이 없습니다.)Windows Store 앱의 CollectionViewSource.SortDescriptors

ObserveableCollectionOrderBy<>()를 사용하는 옵션이 아닌, 내가 동적으로 항목을 추가 (그리고 이것은 ListView의 전체 다시로드를 일으킬 것) 때문이다.

또는이가 CollectionViewSource (를) 대체 할 수있는 다른입니다 (List<>로 가능 것과 유사) ObservableCollection에 I "이진 삽입"어떻게.

+0

'CollectionViewSource.SortDescriptors' 오. –

답변

2

당신은 또한 당신이 클래스를 생성하고이 같은 ObservableCollection.InsertItem 무시할 수있는이 A winRT CollectionView with filtering and sorting

같은 것을 사용할 수 있습니다.

public class SortedCollection<T> : ObservableCollection<T> where T : IComparable<T> 
{ 
    protected override void InsertItem(int index, T item) 
    { 
     int sortedIndex = FindSortedIndex(item); 
     base.InsertItem(sortedIndex, item); 
    } 

    private int FindSortedIndex(T item) 
    { 
     //simple sorting algorithm 
     for (int index = 0; index < this.Count; index++) 
     { 
      if (item.CompareTo(this[index]) > 0) 
      { 
       return index; 
      } 
     } 
     return 0; 
    } 
} 

이 클래스를 사용하려면 새 컬렉션을 만들고 항목을 추가하십시오.

SortedCollection<int> sortedCollection = new SortedCollection<int>(); 
sortedCollection.Add(3); 
sortedCollection.Add(1); 
sortedCollection.Add(5); 
sortedCollection.Add(4); 
sortedCollection.Add(2); 
//the sorted collection will be 1,2,3,4,5 
관련 문제