![](/img/trans.png)
[英]Windows 8 Metro ListView - can't disable ScrollViewer (C#/XAML)
[英]C# Metro (Windows Store) Keeping ListView sorted
保持綁定到按字母順序排序的ListView的ObservableCollection的好的解決方案是什么? 似乎Windows 8.x在CollectionViewSource上不提供排序,因此我將需要對ObservableCollection進行排序。
每次將字符串添加到集合時,集合都必須按照正確的排序順序,因此,我相信我需要的是一種將數據插入適當位置的方法,而不是將其添加到ObservableCollection中,然后對該集合進行排序,但是我還沒有找到執行此操作的好方法。 希望對此有任何建議。
在不影響整個集合的情況下進行插入的一種方法是,首先找到將要插入新項目的索引,然后只需使用內置的Insert
將其添加到集合中即可。
在這種情況下,擴展方法將是完美的。
public static void InsertInOrder<T>(this IList<T> list, T newItem, IComparer<T> comparer = null)
{
comparer = comparer ?? Comparer<T>.Default;
var index = Array.BinarySearch<T>(list.ToArray<T>(), newItem, comparer);
if (index >= 0)
{
throw new ArgumentException("Cannot insert duplicated items");
}
else
{
list.Insert(~index, newItem);
}
}
確保您有這樣的收藏,
ObservableCollection<string> _collection = new ObservableCollection<string> { "a", "b", "c", "e", "f" };
然后調用擴展方法,
_collection.InsertInOrder("d");
希望這可以幫助!
ObservableCollection<T>
實現IList<T>
,因此每次您需要添加一些東西並插入到適當的位置時,都可以對它作為列表進行二進制搜索:
public static class ListHelper
{
public static int BinarySearchFirst<T>(this IList<T> list, T item, IComparer<T> comparer)
{
comparer = comparer ?? Comparer<T>.Default;
int start = list.BinarySearch(item, comparer);
for (; start > 0 && comparer.Compare(list[start], list[start - 1]) == 0; start--)
;
return start;
}
public static int BinarySearchLast<T>(this IList<T> list, T item, IComparer<T> comparer)
{
comparer = comparer ?? Comparer<T>.Default;
int start = list.BinarySearch(item, comparer);
if (start > 0)
{
for (int end = list.Count - 1; start < end && comparer.Compare(list[start], list[start + 1]) == 0; start++)
;
}
return start;
}
public static int BinarySearch<T>(this IList<T> list, T value)
{
return BinarySearch(list, value, null);
}
// Searches the list for a given element using a binary search
// algorithm. Elements of the list are compared to the search value using
// the given IComparer interface. If comparer is null, elements of
// the list are compared to the search value using the IComparable
// interface, which in that case must be implemented by all elements of the
// list and the given search value. This method assumes that the given
// section of the list is already sorted; if this is not the case, the
// result will be incorrect.
//
// The method returns the index of the given value in the list. If the
// list does not contain the given value, the method returns a negative
// integer. The bitwise complement operator (~) can be applied to a
// negative result to produce the index of the first element (if any) that
// is larger than the given search value. This is also the index at which
// the search value should be inserted into the list in order for the list
// to remain sorted.
public static int BinarySearch<T>(this IList<T> list, T value, IComparer<T> comparer)
{
// Adapted from http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs
if (list == null)
throw new ArgumentNullException("list");
comparer = comparer ?? Comparer<T>.Default;
int lo = 0;
int hi = list.Count - 1;
while (lo <= hi)
{
int i = lo + ((hi - lo) >> 1);
int order = comparer.Compare(list[i], value);
if (order == 0)
return i;
if (order < 0)
{
lo = i + 1;
}
else
{
hi = i - 1;
}
}
return ~lo;
}
public static int AddToSortedList<T>(this IList<T> list, T value, bool allowDuplicates)
{
return list.AddToSortedList(value, allowDuplicates, null);
}
public static int AddToSortedList<T>(this IList<T> list, T value, bool allowDuplicates, IComparer<T> comparer)
{
// If the incoming value is equivalent to the some value already in the list using the current comparer,
// add it to the END of the sequence of equivalent entries.
int index = list.BinarySearchLast(value, comparer);
if (!allowDuplicates && index >= 0)
return index;
if (index < 0)
index = ~index;
list.Insert(index, value);
return index;
}
}
AddToSortedList()
應該是擴展方法還是僅僅是一些靜態輔助方法,這是有爭議的,因為許多列表不會被排序。 如果嘗試向列表中添加與某些預先存在的條目等效的條目,則我的方法將新條目添加到等效條目序列的末尾,或者返回現有條目的索引,如呼叫者。 根據您的需要,您可能只想拋出一個異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.