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

前端 未结 10 1152
感动是毒
感动是毒 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 18:13

    Solution that summarizes all working parts of existing answers and comments including column header templates:

    View:

    
        
    
            
    
            
    
            
    
            
                
                    
                
            
    
            
                
                    
                    
                
            
    
            
                
                    
                    
                
            
    
        
    
        
            
    
                
                
    
                
    
            
        
    
    

    Code Behinde:

    public partial class MyListView : ListView
    {
        GridViewColumnHeader _lastHeaderClicked = null;
    
        public MyListView()
        {
            InitializeComponent();
        }
    
        private void ListViewColumnHeaderClick(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
    
            if (headerClicked == null)
                return;
    
            if (headerClicked.Role == GridViewColumnHeaderRole.Padding)
                return;
    
            var sortingColumn = (headerClicked.Column.DisplayMemberBinding as Binding)?.Path?.Path;
            if (sortingColumn == null)
                return;
    
            var direction = ApplySort(Items, sortingColumn);
    
            if (direction == ListSortDirection.Ascending)
            {
                headerClicked.Column.HeaderTemplate =
                    Resources["HeaderTemplateArrowUp"] as DataTemplate;
            }
            else
            {
                headerClicked.Column.HeaderTemplate =
                    Resources["HeaderTemplateArrowDown"] as DataTemplate;
            }
    
            // Remove arrow from previously sorted header
            if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
            {
                _lastHeaderClicked.Column.HeaderTemplate =
                    Resources["HeaderTemplateDefault"] as DataTemplate;
            }
    
            _lastHeaderClicked = headerClicked;
        }
    
    
        public static ListSortDirection ApplySort(ICollectionView view, string propertyName)
        {
            ListSortDirection direction = ListSortDirection.Ascending;
            if (view.SortDescriptions.Count > 0)
            {
                SortDescription currentSort = view.SortDescriptions[0];
                if (currentSort.PropertyName == propertyName)
                {
                    if (currentSort.Direction == ListSortDirection.Ascending)
                        direction = ListSortDirection.Descending;
                    else
                        direction = ListSortDirection.Ascending;
                }
                view.SortDescriptions.Clear();
            }
            if (!string.IsNullOrEmpty(propertyName))
            {
                view.SortDescriptions.Add(new SortDescription(propertyName, direction));
            }
            return direction;
        }
    }
    

提交回复
热议问题