我正在嘗試編寫一個SortableBindingList,可以用於我的應用程序。我發現很多討論如何實現基本排序支持,以便在一個DataGridView或其他一些綁定控件,包括這篇文章從StackOverflow上的上下文中使用時的BindingList將整理:
DataGridView sort and e.g. BindingList<T> in .NETBindingList <T> .Sort()表現得像一個列表<T> .Sort()
這是非常有幫助和我已經實現了代碼,測試等等,它都可以工作,但在我的特殊情況下,我需要能夠支持對Sort()的簡單調用,並讓該調用使用默認的IComparable.CompareTo()來執行排序,而不是調用ApplySortCore(PropertyDescriptor,ListSortDirection)。
原因是因爲我有相當多的代碼取決於Sort()調用,因爲這個特定的類最初是從List繼承的,最近更改爲BindingList。
特別是,我有一個名爲VariableCode的類和一個名爲VariableCodeList的集合類。 VariableCode實現IComparable的,並在那裏適度複雜的基於幾個特性等方面的邏輯......
public class VariableCode : ... IComparable ...
{
public int CompareTo(object p_Target)
{
int output = 0;
//some interesting stuff here
return output;
}
}
public class VariableCodeList : SortableBindingList<VariableCode>
{
public void Sort()
{
//This is where I need help
// How do I sort this list using the IComparable
// logic from the class above?
}
}
我做了一些在排序再利用的方法ApplySortCore()失敗的嘗試,但什麼使挫敗我是ApplySortCore期望一個PropertyDescriptor做它的排序,我不知道如何得到它使用IComparable.CompareTo()邏輯。
有人能指出我正確的方向嗎?
非常感謝。
編輯:這是基於馬克的反應爲未來參考的最終代碼。
/// <summary>
/// Sorts using the default IComparer of T
/// </summary>
public void Sort()
{
sort(null, null);
}
public void Sort(IComparer<T> p_Comparer)
{
sort(p_Comparer, null);
}
public void Sort(Comparison<T> p_Comparison)
{
sort(null, p_Comparison);
}
private void sort(IComparer<T> p_Comparer, Comparison<T> p_Comparison)
{
m_SortProperty = null;
m_SortDirection = ListSortDirection.Ascending;
//Extract items and sort separately
List<T> sortList = new List<T>();
this.ForEach(item => sortList.Add(item));//Extension method for this call
if (p_Comparison == null)
{
sortList.Sort(p_Comparer);
}//if
else
{
sortList.Sort(p_Comparison);
}//else
//Disable notifications, rebuild, and re-enable notifications
bool oldRaise = RaiseListChangedEvents;
RaiseListChangedEvents = false;
try
{
ClearItems();
sortList.ForEach(item => this.Add(item));
}
finally
{
RaiseListChangedEvents = oldRaise;
ResetBindings();
}
}
尼斯,馬克。謝謝。我一直認爲我錯過了一些東西,並且會有一些內置的支持,而不僅僅是執行簡單的解決方案。通知的優點也是如此。我在內部使用列表而不是Array,這樣我就可以支持靈活的基於委託的排序(如列表類支持)。帖子用最終的代碼更新。 –
2009-06-30 15:25:41