diff --git a/src/Avalonia.Controls.DataGrid/Collections/DataGridGroupDescription.cs b/src/Avalonia.Controls.DataGrid/Collections/DataGridGroupDescription.cs index 587dd228a3a..86e2dd3374c 100644 --- a/src/Avalonia.Controls.DataGrid/Collections/DataGridGroupDescription.cs +++ b/src/Avalonia.Controls.DataGrid/Collections/DataGridGroupDescription.cs @@ -136,6 +136,9 @@ protected int ProtectedItemCount } } + internal abstract DataGridCollectionViewGroupInternal Parent { get; } + internal abstract DataGridGroupDescription GroupBy { get; set; } + protected DataGridCollectionViewGroup(object key) { Key = key; @@ -189,7 +192,7 @@ public DataGridCollectionViewGroupInternal(object key, DataGridCollectionViewGro internal int FullCount { get; set; } - internal DataGridGroupDescription GroupBy + internal override DataGridGroupDescription GroupBy { get { return _groupBy; } set @@ -269,7 +272,7 @@ internal object SeedItem } } - private DataGridCollectionViewGroupInternal Parent => _parentGroup; + internal override DataGridCollectionViewGroupInternal Parent => _parentGroup; /// /// Adds the specified item to the collection diff --git a/src/Avalonia.Controls.DataGrid/DataGrid.cs b/src/Avalonia.Controls.DataGrid/DataGrid.cs index 2542b98df38..8e74ae6bedd 100644 --- a/src/Avalonia.Controls.DataGrid/DataGrid.cs +++ b/src/Avalonia.Controls.DataGrid/DataGrid.cs @@ -2644,6 +2644,11 @@ internal void InitializeElements(bool recycleRows) // We want to persist selection throughout a reset, so store away the selected items List selectedItemsCache = new List(_selectedItems.SelectedItemsCache); + var collapsedGroupsCache = RowGroupHeadersTable + .Where(g => !g.Value.IsVisible) + .Select(g => g.Value.CollectionViewGroup.Key) + .ToArray(); + if (recycleRows) { RefreshRows(recycleRows, clearRows: true); @@ -2653,6 +2658,16 @@ internal void InitializeElements(bool recycleRows) RefreshRowsAndColumns(clearRows: true); } + // collapse previously collapsed groups + foreach (var g in collapsedGroupsCache) + { + var item = RowGroupHeadersTable.FirstOrDefault(t => t.Value.CollectionViewGroup.Parent.GroupBy.KeysMatch(t.Value.CollectionViewGroup.Key, g)); + if (item != null) + { + EnsureRowGroupVisibility(item.Value, false, false); + } + } + // Re-select the old items _selectedItems.SelectedItemsCache = selectedItemsCache; CoerceSelectedItem();