Skip to content

Honor template content reuse during row recycling#311

Open
wieslawsoltes wants to merge 2 commits into
masterfrom
agent/issue-290-template-cell-reuse
Open

Honor template content reuse during row recycling#311
wieslawsoltes wants to merge 2 commits into
masterfrom
agent/issue-290-template-cell-reuse

Conversation

@wieslawsoltes

Copy link
Copy Markdown
Owner

Summary

Honor DataGridTemplateColumn.ReuseCellContent when a recycled row is assigned to another ordinary data item, while retaining forced regeneration for template changes and edit-mode transitions.

Fixes #290.

Root cause

DataGridRow.OnPropertyChanged refreshed template cells after a recycled row received a new DataContext by calling RefreshCellContent with nameof(DataGridTemplateColumn.CellTemplate).

That refresh reason is also used for a real template-property change. DataGridTemplateColumn therefore set _forceGenerateCellFromTemplate, discarded the existing control, and rebuilt the template. In practice, every real-to-real row recycle bypassed ReuseCellContent, so the optimization worked in direct GenerateElement tests but not through the actual row lifecycle.

Changes

  • Separate data-context refresh from template-property refresh.
  • Add an internal data-context refresh path that allows the existing cell content to participate in normal reuse/recycling decisions.
  • Keep explicit template changes on a forced-build path so replacing CellTemplate or NewRowCellTemplate cannot retain visuals from the old template.
  • Restrict the one-shot force flag to its original edit-mode responsibility: replacing an editing control with display content when a cell exits edit mode.
  • Preserve the existing placeholder-transition handling, which clears incompatible visuals before moving between a real item and the new-item placeholder.
  • Add an Avalonia Headless lifecycle regression test that recycles a row between two ordinary items and verifies both control identity and template build count.

User and performance impact

Applications opting into ReuseCellContent now receive the intended behavior during virtualization: ordinary row recycling retains the existing control and lets inherited DataContext update it. This avoids repeated template construction in scroll/update paths without changing the default behavior when reuse is disabled.

Validation

  • Template reuse, editing, and container lifecycle suites: 24 passed, 0 failed.
  • Full Avalonia.Controls.DataGrid.UnitTests project: 2,251 passed, 0 failed.
  • git diff --check: clean.

@pull-request-size pull-request-size Bot added size/L and removed size/M labels Jul 15, 2026
@wieslawsoltes

Copy link
Copy Markdown
Owner Author

Self-review completed. I found and fixed an edge case where content created from NewRowCellTemplate could be reused after the placeholder became a regular data row. Commit 86349f7 uses the row recycling placeholder state to force the correct display template and adds regression coverage. Focused tests passed 4/4, the full unit suite passed 2,252/2,252, all six CI checks are successful, and git diff --check is clean.

@wieslawsoltes
wieslawsoltes marked this pull request as ready for review July 15, 2026 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confused about template column cell reuse

1 participant