1
votes

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!

2

2 Answers

1
votes

The DataGrid sorts the view of the List<T> when you click on the column headers without you having to anything at all. This is built-in functionality so you don't have to handle the Sorting event at all. Just set or bind the ItemsSource to your List<T>.

Note that the actual source collection, i.e. the List<T> is not sorted though. Whenever you bind to some collection in WPF, an ICollectionView is automatically created and it is this one that gets sorted by the DataGrid.

1
votes

By doing this:

LibraryView.ItemsSource = _manager.GetLibrarySongs();

you destroy the DataBinding. Actually you shouldn't have to do the sorting at all. If the collection is properly bound to the datagrid, it should be able to sort entries by columns out of the box.