Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid MultiColumn sort without shift key

The WPF DataGrid has a default behavior of allowing multicolumn sorts when the user shift-clicks on multiple column headers. Is there a way to change this behavior so that the Shift key is not required? I have already handled the sorting event on the datagrid so that each column will cycle between the three sorting states (Ascending, Descending, and No Sort) and this is all working as expected as long as the shift key is held down, but I would like to make it so that the DataGrid does not reset the sorting on all other columns if the user clicks a column header to add a sort without pressing shift.

like image 262
aalex675 Avatar asked Dec 01 '25 03:12

aalex675


1 Answers

I have found a solution that seems to be a bit of a hack, but it works. This article got me pointed in the right direction: http://blogs.msdn.com/b/vinsibal/archive/2008/08/29/wpf-datagrid-tri-state-sorting-sample.aspx?PageIndex=2. That brought me to the understanding the the SortDirection of each column is not really tied to the ItemsSource SortDescriptions in any way. So what I did was subscribe to the Sorting event of the Datagrid and reset the SortDirection of each column that is referenced in the ItemsSource SortDescriptions collection. Apparently, not pressing shift clears the sortdirection of each column, but doesn't reset the SortDescriptions.

like image 81
aalex675 Avatar answered Dec 04 '25 02:12

aalex675