Feature/column autofit on double click#225
Open
HannahVernon wants to merge 2 commits into
Open
Conversation
Update Avalonia to 12.0.0-rc2
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>
Author
|
This addresses issue #225 |
|
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'sDataGrid.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):DoubleTappedhandler in the constructorDATAGRIDCOLUMNHEADER_resizeRegionWidthconstant for grip detection — right edge fits that column, left edge fits the column to the leftCanResizeColumn()(both grid-level and column-level settings)Autosizing viaSetWidthInternalNoCallbackto trigger content measurement, then locks to a fixed pixel width viaDispatcher.Postfor predictable subsequent drag behaviorActualMinWidth/ActualMaxWidthconstraintsWhy
This is standard behavior in WPF
DataGrid, WinFormsDataGridView, 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
OwningColumnis internal, estimated grip zone, layout timing hacks). This native implementation avoids all of those by using internal APIs directly.Testing