不带shift键的WPF DataGrid多列排序

不带shift键的WPF DataGrid多列排序,wpf,sorting,datagrid,Wpf,Sorting,Datagrid,WPF DataGrid有一个默认行为,即当用户shift单击多个列标题时,允许进行多列排序。是否有方法更改此行为,以便不需要Shift键?我已经在datagrid上处理了排序事件,这样每个列都会在三种排序状态(升序、降序和无排序)之间循环,只要按住shift键,这一切都会正常工作,但是我想这样做,如果用户在不按shift键的情况下单击列标题添加排序,DataGrid不会重置所有其他列上的排序。我找到了一个解决方案,看起来有点麻烦,但它可以工作。这篇文章为我指明了正确的方向:。这使我了解到,每一

WPF DataGrid有一个默认行为,即当用户shift单击多个列标题时,允许进行多列排序。是否有方法更改此行为,以便不需要Shift键?我已经在datagrid上处理了排序事件,这样每个列都会在三种排序状态(升序、降序和无排序)之间循环,只要按住shift键,这一切都会正常工作,但是我想这样做,如果用户在不按shift键的情况下单击列标题添加排序,DataGrid不会重置所有其他列上的排序。

我找到了一个解决方案,看起来有点麻烦,但它可以工作。这篇文章为我指明了正确的方向:。这使我了解到,每一列的排序方向实际上并不以任何方式与ItemsSource排序描述联系在一起。因此,我所做的是订阅Datagrid的排序事件,并重置ItemsSource SortDescriptions集合中引用的每个列的SortDirection。显然,不按shift键会清除每列的排序方向,但不会重置排序说明。

我正在处理相同的问题,但没有人显示可以解决此问题的代码。这个问题由来已久,但我希望这个解决方案对求职者有用

(DataGrid.Items.SortDescriptions as INotifyCollectionChanged)。CollectionChanged+=OnGridCollectionChanged;
私有void OnGridCollectionChanged(对象发送方,NotifyCollectionChangedEventArgs NotifyCollectionChangedEventArgs)
{
var sortingCollection=(sortddescriptioncollection)发送方;
foreach(sortingCollection中的var sortDesc)
{
foreach(SignaturesInMagingGrid.Columns中的var列)
{
if(column.SortMemberPath.Equals(sortDesc.PropertyName))
{
column.SortDirection=sortDesc.Direction;
}
}
}
}
公共静态void GridMultiColumnSortingEvent(对象发送方,DataGridSortingEventArgs e)
{
var dgSender=(数据网格)发送方;
var cView=CollectionViewSource.GetDefaultView(dgSender.ItemsSource);
ListSortDirection方向=ListSortDirection.升序;
if(ContainsSortColumn((DataGrid)发送方,例如Column.SortMemberPath))
{
if(e.Column.SortDirection==null)
{
方向=列表或方向。升序;
ChangeSortColumn((DataGrid)发送方,如列、方向);
}
else if(DirectionForColumn(cView,e.Column)=ListSortDirection.升序)
{
方向=列表或方向。下降;
ChangeSortColumn((DataGrid)发送方,如列、方向);
}
else if(DirectionForColumn(cView,e.Column)=ListSortDirection.Descending)
{
e、 Column.SortDirection=null;
cView.SortDescriptions.Remove(cView.SortDescriptions.Where(item=>item.PropertyName.Equals(e.Column.SortMemberPath)).FirstOrDefault());
cView.Refresh();
}
}
其他的
{
AddSortColumn((DataGrid)发送方,例如Column.SortMemberPath,direction);
cView.Refresh();
}
e、 已处理=正确;
}
专用静态列表或列的方向(ICollectionView cView,DataGridColumn column)=>
其中(item=>item.PropertyName.Equals(column.SortMemberPath))
.FirstOrDefault()
.方向;
私有静态void AddSortColumn(数据网格发送器、字符串sortColumn、列表SortDirection方向)
{
var cView=CollectionViewSource.GetDefaultView(sender.ItemsSource);
cView.SortDescriptions.Add(新的SortDescription(sortColumn,direction));
foreach(sender.Columns.Where(x=>x.SortMemberPath==sortColumn)中的变量col)
{
col.SortDirection=方向;
}
}
私有静态void ChangeSortColumn(DataGrid发送方、DataGridColumn列、ListSortDirection方向)
{
var cView=CollectionViewSource.GetDefaultView(sender.ItemsSource);
字符串sortColumn=column.SortMemberPath;
foreach(cView.sortddescriptions.ToList()中的var sortDesc)
{
if(sortDesc.PropertyName.Equals(sortColumn))
{
cView.SortDescriptions.Remove(sortDesc);
打破
}
}
AddSortColumn(发送方、sortColumn、方向);
}
私有静态bool containersortColumn(数据网格发送器、字符串sortColumn)
{
var cView=CollectionViewSource.GetDefaultView(sender.ItemsSource);
foreach(cView.sortddescriptions.ToList()中的var sortDesc)
{
if(sortDesc.PropertyName.Equals(sortColumn))
返回true;
}
返回false;
}

页面已被移动:我是搜索者:您应该使用
CollectionViewSource.GetDefaultView(dgSender.Items)
避免创建不必要的新视图。