Skip to content

Feature/column autofit on double click#225

Open
HannahVernon wants to merge 2 commits into
AvaloniaUI:update-av-12.0.0-rc2from
HannahVernon:feature/column-autofit-on-double-click
Open

Feature/column autofit on double click#225
HannahVernon wants to merge 2 commits into
AvaloniaUI:update-av-12.0.0-rc2from
HannahVernon:feature/column-autofit-on-double-click

Conversation

@HannahVernon
Copy link
Copy Markdown

Auto-fit column width on double-click of resize grip

What

Adds the standard "double-click column separator to auto-fit" behavior to DataGridColumnHeader, matching WPF's DataGrid.

Before: Double-clicking the resize grip does nothing (or triggers sort on the adjacent header).
After: Double-clicking the resize grip auto-sizes the column to fit its header and visible cell content.

How

Single addition to DataGridColumnHeader.cs (~60 lines, no API surface changes):

  1. New DoubleTapped handler in the constructor
  2. Uses the existing DATAGRIDCOLUMNHEADER_resizeRegionWidth constant for grip detection — right edge fits that column, left edge fits the column to the left
  3. Respects CanResizeColumn() (both grid-level and column-level settings)
  4. Temporarily sets Auto sizing via SetWidthInternalNoCallback to trigger content measurement, then locks to a fixed pixel width via Dispatcher.Post for predictable subsequent drag behavior
  5. Respects ActualMinWidth / ActualMaxWidth constraints

Why

This is standard behavior in WPF DataGrid, WinForms DataGridView, Excel, and virtually every desktop grid control. Users coming from any of these environments instinctively double-click column separators to auto-fit. I searched the Avalonia and Avalonia.Controls.DataGrid issue trackers and found no prior feature request.

We implemented an external workaround for our project, but it has limitations (string-based column matching since OwningColumn is internal, estimated grip zone, layout timing hacks). This native implementation avoids all of those by using internal APIs directly.

Testing

  • All 20 existing unit tests pass
  • Manually verified in our Avalonia 11.x app via the external workaround using the same approach

MrJul and others added 2 commits March 31, 2026 22:24
Double-clicking the column header resize grip now auto-sizes the column
to fit its content, matching WPF DataGrid behavior.

- Right edge double-click auto-fits that column
- Left edge double-click auto-fits the column to the left
- Respects CanUserResizeColumns / CanUserResize constraints
- Respects MinWidth / MaxWidth constraints
- Uses the existing DATAGRIDCOLUMNHEADER_resizeRegionWidth (5px) for
  consistent grip zone detection
- Temporarily switches to Auto sizing to measure content, then locks
  to a fixed pixel width for predictable subsequent drag-resizing

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@HannahVernon
Copy link
Copy Markdown
Author

This addresses issue #225

@kdurane
Copy link
Copy Markdown

kdurane commented Apr 28, 2026

I added you double tap method to my fork, as I agree, its a great feature to have, however, I wasn't able to make it work. I did make it work by asking the datagrid to do it itself, by using OwningColumn.SetWidthInternalNoCallback(DataGridLength.Auto);
columnToAutoFit.OwningGrid?.InvalidateMeasure(); (at the end of your method) instead of AutoFitColumnWidth method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants