|
|
|
@ -161,13 +161,14 @@ namespace Avalonia.Controls |
|
|
|
var sort = OwningColumn.GetSortDescription(); |
|
|
|
if (sort != null) |
|
|
|
{ |
|
|
|
CurrentSortingState = sort.Descending ? ListSortDirection.Descending : ListSortDirection.Ascending; |
|
|
|
CurrentSortingState = sort.Direction; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
PseudoClasses.Set(":sortascending", |
|
|
|
CurrentSortingState.HasValue && CurrentSortingState.Value == ListSortDirection.Ascending); |
|
|
|
CurrentSortingState == ListSortDirection.Ascending); |
|
|
|
PseudoClasses.Set(":sortdescending", |
|
|
|
CurrentSortingState.HasValue && CurrentSortingState.Value == ListSortDirection.Descending); |
|
|
|
CurrentSortingState == ListSortDirection.Descending); |
|
|
|
} |
|
|
|
|
|
|
|
internal void UpdateSeparatorVisibility(DataGridColumn lastVisibleColumn) |
|
|
|
@ -215,70 +216,76 @@ namespace Avalonia.Controls |
|
|
|
internal void ProcessSort(KeyModifiers keyModifiers) |
|
|
|
{ |
|
|
|
// if we can sort:
|
|
|
|
// - DataConnection.AllowSort is true, and
|
|
|
|
// - AllowUserToSortColumns and CanSort are true, and
|
|
|
|
// - OwningColumn is bound, and
|
|
|
|
// - SortDescriptionsCollection exists, and
|
|
|
|
// - the column's data type is comparable
|
|
|
|
// - OwningColumn is bound
|
|
|
|
// then try to sort
|
|
|
|
if (OwningColumn != null |
|
|
|
&& OwningGrid != null |
|
|
|
&& OwningGrid.EditingRow == null |
|
|
|
&& OwningColumn != OwningGrid.ColumnsInternal.FillerColumn |
|
|
|
&& OwningGrid.DataConnection.AllowSort |
|
|
|
&& OwningGrid.CanUserSortColumns |
|
|
|
&& OwningColumn.CanUserSort |
|
|
|
&& OwningGrid.DataConnection.SortDescriptions != null) |
|
|
|
&& OwningColumn.CanUserSort) |
|
|
|
{ |
|
|
|
DataGrid owningGrid = OwningGrid; |
|
|
|
var ea = new DataGridColumnEventArgs(OwningColumn); |
|
|
|
OwningGrid.OnColumnSorting(ea); |
|
|
|
|
|
|
|
DataGridSortDescription newSort; |
|
|
|
if (!ea.Handled && OwningGrid.DataConnection.AllowSort && OwningGrid.DataConnection.SortDescriptions != null) |
|
|
|
{ |
|
|
|
// - DataConnection.AllowSort is true, and
|
|
|
|
// - SortDescriptionsCollection exists, and
|
|
|
|
// - the column's data type is comparable
|
|
|
|
|
|
|
|
KeyboardHelper.GetMetaKeyState(keyModifiers, out bool ctrl, out bool shift); |
|
|
|
DataGrid owningGrid = OwningGrid; |
|
|
|
DataGridSortDescription newSort; |
|
|
|
|
|
|
|
DataGridSortDescription sort = OwningColumn.GetSortDescription(); |
|
|
|
IDataGridCollectionView collectionView = owningGrid.DataConnection.CollectionView; |
|
|
|
Debug.Assert(collectionView != null); |
|
|
|
using (collectionView.DeferRefresh()) |
|
|
|
{ |
|
|
|
// if shift is held down, we multi-sort, therefore if it isn't, we'll clear the sorts beforehand
|
|
|
|
if (!shift || owningGrid.DataConnection.SortDescriptions.Count == 0) |
|
|
|
{ |
|
|
|
owningGrid.DataConnection.SortDescriptions.Clear(); |
|
|
|
} |
|
|
|
KeyboardHelper.GetMetaKeyState(keyModifiers, out bool ctrl, out bool shift); |
|
|
|
|
|
|
|
DataGridSortDescription sort = OwningColumn.GetSortDescription(); |
|
|
|
IDataGridCollectionView collectionView = owningGrid.DataConnection.CollectionView; |
|
|
|
Debug.Assert(collectionView != null); |
|
|
|
|
|
|
|
// if ctrl is held down, we only clear the sort directions
|
|
|
|
if (!ctrl) |
|
|
|
using (collectionView.DeferRefresh()) |
|
|
|
{ |
|
|
|
if (sort != null) |
|
|
|
// if shift is held down, we multi-sort, therefore if it isn't, we'll clear the sorts beforehand
|
|
|
|
if (!shift || owningGrid.DataConnection.SortDescriptions.Count == 0) |
|
|
|
{ |
|
|
|
newSort = sort.SwitchSortDirection(); |
|
|
|
owningGrid.DataConnection.SortDescriptions.Clear(); |
|
|
|
} |
|
|
|
|
|
|
|
// changing direction should not affect sort order, so we replace this column's
|
|
|
|
// sort description instead of just adding it to the end of the collection
|
|
|
|
int oldIndex = owningGrid.DataConnection.SortDescriptions.IndexOf(sort); |
|
|
|
if (oldIndex >= 0) |
|
|
|
// if ctrl is held down, we only clear the sort directions
|
|
|
|
if (!ctrl) |
|
|
|
{ |
|
|
|
if (sort != null) |
|
|
|
{ |
|
|
|
owningGrid.DataConnection.SortDescriptions.Remove(sort); |
|
|
|
owningGrid.DataConnection.SortDescriptions.Insert(oldIndex, newSort); |
|
|
|
newSort = sort.SwitchSortDirection(); |
|
|
|
|
|
|
|
// changing direction should not affect sort order, so we replace this column's
|
|
|
|
// sort description instead of just adding it to the end of the collection
|
|
|
|
int oldIndex = owningGrid.DataConnection.SortDescriptions.IndexOf(sort); |
|
|
|
if (oldIndex >= 0) |
|
|
|
{ |
|
|
|
owningGrid.DataConnection.SortDescriptions.Remove(sort); |
|
|
|
owningGrid.DataConnection.SortDescriptions.Insert(oldIndex, newSort); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
owningGrid.DataConnection.SortDescriptions.Add(newSort); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
string propertyName = OwningColumn.GetSortPropertyName(); |
|
|
|
// no-opt if we couldn't find a property to sort on
|
|
|
|
if (string.IsNullOrEmpty(propertyName)) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
newSort = DataGridSortDescription.FromPath(propertyName, culture: collectionView.Culture); |
|
|
|
|
|
|
|
owningGrid.DataConnection.SortDescriptions.Add(newSort); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
string propertyName = OwningColumn.GetSortPropertyName(); |
|
|
|
// no-opt if we couldn't find a property to sort on
|
|
|
|
if (string.IsNullOrEmpty(propertyName)) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
newSort = DataGridSortDescription.FromPath(propertyName, culture: collectionView.Culture); |
|
|
|
owningGrid.DataConnection.SortDescriptions.Add(newSort); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|