我正在尝试编写一个可用于我的应用程序的SortableBindingList.我发现了很多关于如何实现基本排序支持的讨论,以便BindingList在DataGridView或其他绑定控件的上下文中使用时进行排序,包括来自StackOverflow的这篇文章:
DataGridView排序和例如.NET中的BindingList <T>
这一切都非常有用,我已经实现了代码,经过测试等等,并且一切正常,但在我的特殊情况下,我需要能够支持对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?
}
}
Run Code Online (Sandbox Code Playgroud)
我在Sort()中重新调整了ApplySortCore方法的尝试失败了,但是阻止我的是,ApplySortCore期望PropertyDescriptor进行排序,我无法弄清楚如何使用IComparable .CompareTo()逻辑.
有人能指出我正确的方向吗?
非常感谢.
编辑:这是基于Marc的回复的最终代码,供将来参考.
/// <summary>
/// Sorts using the default IComparer of …Run Code Online (Sandbox Code Playgroud)