0

I want to sort by clicking on a column. A simple task on Windows Form....

Following the sample of the answer on WPF ListView sorting on column click

GridViewColumnHeader _lastHeaderClicked = null;
ListSortDirection _lastDirection = ListSortDirection.Ascending;

void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
    {
        GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
        ListSortDirection direction;

        MessageBox.Show("Clic detected on column: " + headerClicked);

        if (headerClicked != null)
        {
            if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
            {
                if (headerClicked != _lastHeaderClicked)
                {
                    direction = ListSortDirection.Ascending;
                }
                else
                {
                    if (_lastDirection == ListSortDirection.Ascending)
                    {
                        direction = ListSortDirection.Descending;
                    }
                    else
                    {
                        direction = ListSortDirection.Ascending;
                    }
                }

                string header = headerClicked.Column.Header as string;
                Sort(header, direction);

                _lastHeaderClicked = headerClicked;
                _lastDirection = direction;
            }
        }
    }

    private void Sort(string sortBy, ListSortDirection direction)
    {

        MessageBox.Show("I'm inside of the Sort!");

        ICollectionView dataView =
          CollectionViewSource.GetDefaultView(listView.ItemsSource);

        dataView.SortDescriptions.Clear();
        SortDescription sd = new SortDescription(sortBy, direction);
        dataView.SortDescriptions.Add(sd);
        dataView.Refresh();
    }

I'm using both MessageBox to check if the column is selected and works fine, even with the internal name of the column, but the listview remain the same. No error, no sort.

What is wrong?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
fedeteka
  • 943
  • 1
  • 14
  • 33

1 Answers1

1

That is the tried and tested "ListView sort on column header click" code that you see repeated not only on stackoverflow but also throughout many of the repositories that have extended ListView. I strongly suspect your issue is not with the above code.

ATH
  • 36
  • 4
  • Sure, but I don't know what I'm missing. Xaml on the Listview? Some properties to change? – fedeteka Jan 26 '18 at 11:00
  • 1
    Yes it could be some issue with the xaml/binding. I would have a look at the [MSDN page](https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-sort-a-gridview-column-when-a-header-is-clicked) and try to get that example working. As I said this code is repeated a lot throughout other repos, so you could also find one on github and have a look at how its implemented there. Also make sure that whatever property you are sorting sorts in the way you'd expect (implements IComparable if its a custom class). – ATH Jan 29 '18 at 19:53