私のアプリケーションに使用できるSortableBindingListを書き込もうとしています。
DataGridView sort and e.g. BindingList<T> in .NETBindingList <T>リストのように動作する.Sort()<T> .Sort()
これはすべての非常に有用である:私はDataGridViewのかStackOverflowのからこの記事を含むいくつかの他のバインドされたコントロールのコンテキストで使用する場合するBindingListがソートされますように、基本的なソートのサポートを実装する方法についての議論の多くを発見しました私はコードを実装し、テストしたなど、それはすべて動作しますが、私の特定の状況では、私はSort()への単純な呼び出しをサポートし、その呼び出しにデフォルトのIComparable.CompareTo()を使用する必要がありますApplySortCore(PropertyDescriptor、ListSortDirection)を呼び出すのではなく、並べ替えを行います。
なぜなら、この特定のクラスはもともとListから継承され、最近BindingListに変更されたため、Sort()呼び出しに依存するかなりのコードがあるからです。
具体的には、私は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?
}
}
I)は、(いくつかの並べ替えにApplySortCore方法を再利用する試みが失敗した作りましたが、何を阻止し続けます私は、ApplySortCoreがPropertyDescriptorのソートを期待しているため、IComparable.CompareTo()ロジックを使用する方法を理解できません。
誰かが正しい方向に向かうことができますか?
多くのありがとうございます。
編集:これはMarcのレスポンスに基づいて作成されたもので、今後の参考になるものです。
/// <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();
}
}
Nice、Marc。ありがとうございました。私は何かが欠けていると思っていたし、簡単な解決策を実行するだけではなく、どこかにサポートが組み込まれていると思っていました。通知の良い点も同様です。 Listクラスをサポートするように、フレキシブルなデリゲートベースの並べ替えをサポートできるように、配列ではなくリストを使って巻きました。投稿は最終コードで更新されます。 –