Best way to make WPF ListView/GridView sort on column-header clicking?

前端 未结 10 1150
感动是毒
感动是毒 2020-11-29 17:12

There are lots of solutions on the internet attempting to fill this seemingly very-basic omission from WPF. I\'m really confused as to what would be the \"best\" wa

10条回答
  •  青春惊慌失措
    2020-11-29 17:59

    Just wanted to add another simple way someone can sort the the WPF ListView

    void SortListView(ListView listView)
    {
        IEnumerable listView_items = listView.Items.SourceCollection;
        List listView_items_to_list = listView_items.Cast().ToList();
    
        Comparer scoreComparer = Comparer.Create((first, second) => first.COLUMN_NAME.CompareTo(second.COLUMN_NAME));
    
        listView_items_to_list.Sort(scoreComparer);
        listView.ItemsSource = null;
        listView.Items.Clear();
        listView.ItemsSource = listView_items_to_list;
    }
    

提交回复
热议问题