Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid - preserve groups collapsed state #18331

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ protected int ProtectedItemCount
}
}

internal abstract DataGridCollectionViewGroupInternal Parent { get; }
internal abstract DataGridGroupDescription GroupBy { get; set; }

protected DataGridCollectionViewGroup(object key)
{
Key = key;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -269,7 +272,7 @@ internal object SeedItem
}
}

private DataGridCollectionViewGroupInternal Parent => _parentGroup;
internal override DataGridCollectionViewGroupInternal Parent => _parentGroup;

/// <summary>
/// Adds the specified item to the collection
Expand Down
15 changes: 15 additions & 0 deletions src/Avalonia.Controls.DataGrid/DataGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,11 @@ internal void InitializeElements(bool recycleRows)
// We want to persist selection throughout a reset, so store away the selected items
List<object> selectedItemsCache = new List<object>(_selectedItems.SelectedItemsCache);

var collapsedGroupsCache = RowGroupHeadersTable
.Where(g => !g.Value.IsVisible)
.Select(g => g.Value.CollectionViewGroup.Key)
.ToArray();

if (recycleRows)
{
RefreshRows(recycleRows, clearRows: true);
Expand All @@ -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();
Expand Down
Loading