2

ListBox の並べ替えを可能にする動作を作成しました。正しく機能するには、ListBox の ItemsSource が ObservableCollection<...> でなければならないので、Move(from,to) メソッドを呼び出すことができます。

私の問題は、ListBox.ItemsSource を ObservableCollection にキャストするにはどうすればよいかということです。

私はすでに試しました:

ObservableCollection<object> test = listBox.ItemsSource as ObservableCollection<object>;

ObservableCollection は共分散をサポートしていないため、これは機能しません。

4

1 に答える 1

2

呼び出したいメソッドがわかっているので、ObservableCollection<T>.Move単純なリフレクションを使用できます。

var move = listBox.ItemsSource
                  .GetType()
                  .GetMethod("Move");
if (move != null)
{
    move.Invoke(listBox.ItemsSource, new[] { old, new });
}
else
{
    // IList fallback?
}
于 2012-04-27T08:56:36.277 に答える