I'm trying to sort a List<> that is binded to a WPF datagrid. On first load, it's totally unsorted, but when you click once on the header, it should switch between ascending and descending. The weird thing is, the List<> sorts, and when I re-bind the List<> to the Itemssource, and refresh, etc... It still shows the ascending order. But when I set a breakpoint and go see what's in the ItemsSource, the items ARE sorted?! It just doesn't show in the datagrid for whatever reason. Any idea on how this can happen?
SortingEvent of DataGrid (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:
private void LoadLibrary()
{
if (_manager.CheckLibrary())
{
LibraryView.ItemsSource = _manager.GetLibrarySongs();
}
}
Sorting itself:
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;
}
}
I know there are tons of topics on this, but everything I came across, still doesn't fix this. I don't have much experience with WPF so if I'm doing anything wrong, or bad practice, please let me know. Thanks in advance!