WPFデータグリッドにバインドされたリスト<をソートしようとしています。最初の読み込み時は完全にソートされていませんが、ヘッダーを1回クリックすると、昇順と降順の間で切り替わります。変わったことは、リスト<>ソートであり、リスト<をItemSourceに再バインドしてリフレッシュするときなど...それでも昇順が表示されます。しかし、私がブレークポイントを設定し、ItemsSourceにあるものを見ると、アイテムはソートされますか?どのような理由であれ、データグリッドには表示されません。どのようにこれが起こることができるかについての考え?ソート後にWPFデータグリッドが更新されない
データグリッドのSortingEvent(LibraryView)
private void LibraryView_Sorting(object sender, DataGridSortingEventArgs e)
{
var sortDirection = e.Column.SortDirection;
switch (sortDirection)
{
default:
case ListSortDirection.Descending: //not needed, but makes things clearer
sortDirection = ListSortDirection.Ascending;
break;
case ListSortDirection.Ascending:
sortDirection = ListSortDirection.Descending;
break;
}
_manager.SortLibrary(e.Column.SortMemberPath, sortDirection);
//LibraryView.Items.Clear(); //tried this
LibraryView.ItemsSource = null; //tried this
LoadLibrary();
LibraryView.Items.Refresh(); //tried this
}
のLoadLibrary:
public void SortLibrary(string member, ListSortDirection? sortDirection)
{
PropertyDescriptor prop = TypeDescriptor.GetProperties(typeof(Song)).Find(member, true);
switch (sortDirection)
{
default:
case ListSortDirection.Descending: //not needed, but makes things clearer
_library.Songs = _library.Songs.OrderByDescending(s => prop.GetValue(s)).ToList();
Debug.WriteLine("Sorting descending!!!!");
break;
case ListSortDirection.Ascending:
_library.Songs = _library.Songs.OrderBy(s => prop.GetValue(s)).ToList();
Debug.WriteLine("Sorting ascencding!!!!");
break;
}
}
私はこの上の話題のトンがある知っているが、すべては私が来た:
private void LoadLibrary()
{
if (_manager.CheckLibrary())
{
LibraryView.ItemsSource = _manager.GetLibrarySongs();
}
}
自体をソートそれでも、これは修正されません。 私はWPFに関する多くの経験がないので、何か間違ったことや悪い習慣をしているなら、私に知らせてください。 ありがとうございます!これにより
私はそれを調べましたが、以下の行で汎用リストをデータグリッドにバインドしました:LibraryView.SetValue(DataGrid.ItemsSourceProperty、_manager.Library.Songs); ICollectionViewを使用して、ビューのソートをバインドされた汎用リストにミラーリングするにはどうすればよいですか? – JC97
別の問題がある場合は、新しい質問をしてください。コメント欄にその他の質問をしないでください。 – mm8