From 20b880cb5a06229694cdc3c48616b716bb43a26b Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Sat, 21 Nov 2026 00:43:38 +0700 Subject: [PATCH 1/3] fix(windows-datagrid): open a new editor for each edit so the last cell's height never carries over --- .../docs/decisions/0003-data-grid-control.md | 1 + .../TablePro.DataGrid/DataGridView.Editing.cs | 39 ++++++---------- .../TablePro.DataGrid.Tests/EditingTests.cs | 45 ++++++++++++++++--- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/windows/docs/decisions/0003-data-grid-control.md b/windows/docs/decisions/0003-data-grid-control.md index 58644ea637..f716115db9 100644 --- a/windows/docs/decisions/0003-data-grid-control.md +++ b/windows/docs/decisions/0003-data-grid-control.md @@ -75,6 +75,7 @@ - Off until the host sets `IsEditable`. F2, Enter or a double-click on a cell opens the editor; typing does not, as on macOS. `CellEditStarting` gives the host the cell, and the host sets the text or cancels. Text with NUL opens nothing, since a TextBox cannot hold it. - F2 and Enter, and Tab in the editor, open the editor once the key is done, so the key's character does not reach the new TextBox. - The editor is a stock TextBox in the cell's pane, the frozen one or the scrolling one, so it scrolls with the rows. It takes the cells' font, gets focus and selects its text. A value with a line break opens with AcceptsReturn and grows to four rows. +- Each edit opens a new TextBox. A TextBox with focus and no Height that wraps or takes returns animates its own Height for 100 ms when its size changes, and sets Height to NaN when that ends (`TextBox::OnSizeChanged`, `TextBox_Partial.cpp`). One reused for the next cell opened at the last cell's height, and a one-line editor lost the grid's Height a moment later (run 44783975649). - Enter commits and stays. Tab and Shift+Tab commit and edit the next cell to the right or left, as NSTableView does. Escape cancels. Ctrl+Enter commits a value with line breaks. - Losing focus commits, and a refused commit then cancels. Focus that moves to the TextBox's own menus does not. Unloading the grid and setting `FrozenColumnCount` do the same. - On those paths the editor's ToolTip closes with it, so the host shows a refused commit's Error itself. diff --git a/windows/src/TablePro.DataGrid/DataGridView.Editing.cs b/windows/src/TablePro.DataGrid/DataGridView.Editing.cs index 89c919989c..fd3604815f 100644 --- a/windows/src/TablePro.DataGrid/DataGridView.Editing.cs +++ b/windows/src/TablePro.DataGrid/DataGridView.Editing.cs @@ -345,16 +345,15 @@ private void CommitOrCancel(bool refocus) private void OpenEditor(int row, int column, string text) { ClearCellTip(); - var editor = _editor ??= CreateEditor(); - var window = column < _columns.FrozenCount ? _frozenWindow : _window; - if (!ReferenceEquals(editor.Parent, window)) + // A TextBox animates its Height from the last size it showed and then sets it to NaN, so no edit reuses one. + if (_editor is { Parent: Panel pane } last) { - (editor.Parent as Panel)?.Children.Remove(editor); - window.Children.Add(editor); + pane.Children.Remove(last); } + var editor = _editor = CreateEditor(); + (column < _columns.FrozenCount ? _frozenWindow : _window).Children.Add(editor); _editSession++; _edit = (row, column); - ClearEditError(); // Before the text: a TextBox that takes no returns keeps only the first line. editor.AcceptsReturn = text.AsSpan().IndexOfAny('\r', '\n') >= 0; _newline = text.Contains("\r\n", StringComparison.Ordinal) ? "\r\n" @@ -364,27 +363,15 @@ private void OpenEditor(int row, int column, string text) // What the TextBox holds, so a value with mixed line breaks opened and left is no change. _editText = EditorText(editor); AutomationProperties.SetName(editor, _names[column]); - editor.Visibility = Visibility.Visible; PlaceEditor(); - if (editor.IsLoaded) - { - FocusEditor(editor); - } - else - { - editor.Loaded -= OnEditorLoaded; - editor.Loaded += OnEditorLoaded; - } } - // A new editor, or one moved to the other pane, takes focus once it is in the tree. + // The editor takes focus once it is in the tree; a box whose edit has already ended does not. private void OnEditorLoaded(object sender, RoutedEventArgs e) { - var editor = (TextBox)sender; - editor.Loaded -= OnEditorLoaded; - if (IsEditing) + if (IsEditing && ReferenceEquals(sender, _editor)) { - FocusEditor(editor); + FocusEditor((TextBox)sender); } } @@ -463,17 +450,17 @@ private TextBox CreateEditor() MinWidth = 0, MinHeight = 0, TextWrapping = TextWrapping.NoWrap, - Visibility = Visibility.Collapsed, }; ApplyEditorFont(editor); + editor.Loaded += OnEditorLoaded; editor.PreviewKeyDown += OnEditorKeyDown; // Arrows and paging the TextBox leaves would reach the ScrollViewer around it and scroll the grid. editor.KeyDown += (_, e) => e.Handled |= e.Key is VirtualKey.Up or VirtualKey.Down or VirtualKey.Left or VirtualKey.Right or VirtualKey.PageUp or VirtualKey.PageDown or VirtualKey.Home or VirtualKey.End; editor.LostFocus += OnEditorLostFocus; - editor.TextChanged += (_, _) => + editor.TextChanged += (sender, _) => { - if (IsEditing) + if (IsEditing && ReferenceEquals(sender, _editor)) { PendingEditChanged?.Invoke(this, EventArgs.Empty); } @@ -498,10 +485,10 @@ private void OnEditorKeyDown(object sender, KeyRoutedEventArgs e) } } - // Focus moving to the editor's own menus comes back. + // Focus moving to the editor's own menus comes back. The event comes late, so it may be from the last edit's box. private void OnEditorLostFocus(object sender, RoutedEventArgs e) { - if (_editor is null || !IsEditing || EditorFocused + if (_editor is null || !ReferenceEquals(sender, _editor) || !IsEditing || EditorFocused || _editor.ContextFlyout is { IsOpen: true } || _editor.SelectionFlyout is { IsOpen: true }) { return; diff --git a/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs b/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs index 27abf2bf1a..d0948c61d4 100644 --- a/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs +++ b/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs @@ -81,6 +81,39 @@ public Task TheEditorCoversAWrappedRowAndWrapsItsTextAndWithWrapOffIsOneRowAgain grid.CancelEdit(); }); + // A TextBox with focus animates its own Height from the last size it showed, then sets Height to NaN. + [Fact] + public Task EachEditOpensAtItsOwnRowsHeightAfterATallerOne() => WinUiHost.RunAsync(async () => + { + var grid = await WrapChecks.ShowAsync(new WrapData()); + grid.IsEditable = true; + grid.SelectCell(0, 1, extend: false); + grid.BeginEdit(); + await SettleAsync(grid); + Assert.True(grid.Editor!.ActualHeight >= grid.RowHeightOf(0) - 1); + Assert.True(grid.RowHeightOf(0) > grid.RowHeightOf(2) + 1); + grid.CancelEdit(); + + grid.SelectCell(2, 1, extend: false); + grid.BeginEdit(); + await SettleAsync(grid); + Assert.Equal(grid.RowHeightOf(2), grid.Editor!.ActualHeight, 1.0); + grid.CancelEdit(); + + grid.WrapsText = false; + await SettleAsync(grid); + grid.BeginEdit(); + await SettleAsync(grid); + var clock = System.Diagnostics.Stopwatch.StartNew(); + while (clock.ElapsedMilliseconds < 300) + { + await WinUiHost.NextFrameAsync(); + } + Assert.Equal(grid.RowHeight, grid.Editor!.Height); + Assert.Equal(grid.RowHeight, grid.Editor!.ActualHeight, 1.0); + grid.CancelEdit(); + }); + [Fact] public Task TurningWrapOnKeepsWhatTheOpenEditorHolds() => WinUiHost.RunAsync(async () => { @@ -235,22 +268,21 @@ public Task TabCommitsAndEditsTheNextCell() => WinUiHost.RunAsync(async () => grid.BeginEdit(); await SettleAsync(grid); - var editor = grid.Editor!; - editor.Text = "four"; + grid.Editor!.Text = "four"; Assert.True(grid.HandleEditorKey(VirtualKey.Tab, control: false, shift: false)); await SettleAsync(grid); Assert.Equal((3, 4, "four"), Assert.Single(commits)); Assert.True(grid.IsEditing); Assert.Equal((3, 5), (grid.FocusedRow, grid.FocusedColumn)); - Assert.Equal(SyntheticData.Value(3, 5), editor.Text); + Assert.Equal(SyntheticData.Value(3, 5), grid.Editor!.Text); Assert.True(grid.HandleEditorKey(VirtualKey.Tab, control: false, shift: true)); Assert.Equal((3, 4), (grid.FocusedRow, grid.FocusedColumn)); Assert.False(grid.IsEditing); await SettleAsync(grid); Assert.True(grid.IsEditing); - Assert.Equal("r3c4", editor.Text); + Assert.Equal("r3c4", grid.Editor!.Text); Assert.Single(commits); grid.CancelEdit(); }); @@ -277,15 +309,14 @@ public Task AValueWithMixedLineBreaksOpenedAndLeftRaisesNothing() => WinUiHost.R grid.BeginEdit(); await SettleAsync(grid); - var editor = grid.Editor!; - Assert.True(editor.AcceptsReturn); + Assert.True(grid.Editor!.AcceptsReturn); Assert.True(grid.HandleEditorKey(VirtualKey.Enter, control: true, shift: false)); Assert.Empty(commits); Assert.False(grid.IsEditing); grid.BeginEdit(); await SettleAsync(grid); - editor.Text = "a\rb"; + grid.Editor!.Text = "a\rb"; Assert.True(grid.HandleEditorKey(VirtualKey.Enter, control: true, shift: false)); Assert.Equal("a\r\nb", Assert.Single(commits)); }); From f1f2f53e1f6dd30ee5e9664eb995cf88e7c4e873 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Sat, 21 Nov 2026 00:52:16 +0700 Subject: [PATCH 2/3] fix(windows-datagrid): focus the new editor as it opens, not on Loaded --- .../docs/decisions/0003-data-grid-control.md | 2 +- .../TablePro.DataGrid/DataGridView.Editing.cs | 10 +++++++--- .../TablePro.DataGrid.Tests/EditingTests.cs | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/windows/docs/decisions/0003-data-grid-control.md b/windows/docs/decisions/0003-data-grid-control.md index f716115db9..4672fa07d9 100644 --- a/windows/docs/decisions/0003-data-grid-control.md +++ b/windows/docs/decisions/0003-data-grid-control.md @@ -75,7 +75,7 @@ - Off until the host sets `IsEditable`. F2, Enter or a double-click on a cell opens the editor; typing does not, as on macOS. `CellEditStarting` gives the host the cell, and the host sets the text or cancels. Text with NUL opens nothing, since a TextBox cannot hold it. - F2 and Enter, and Tab in the editor, open the editor once the key is done, so the key's character does not reach the new TextBox. - The editor is a stock TextBox in the cell's pane, the frozen one or the scrolling one, so it scrolls with the rows. It takes the cells' font, gets focus and selects its text. A value with a line break opens with AcceptsReturn and grows to four rows. -- Each edit opens a new TextBox. A TextBox with focus and no Height that wraps or takes returns animates its own Height for 100 ms when its size changes, and sets Height to NaN when that ends (`TextBox::OnSizeChanged`, `TextBox_Partial.cpp`). One reused for the next cell opened at the last cell's height, and a one-line editor lost the grid's Height a moment later (run 44783975649). +- Each edit opens a new TextBox. A TextBox with focus and no Height that wraps or takes returns animates its own Height for 100 ms when its size changes, and sets Height to NaN when that ends (`TextBox::OnSizeChanged`, `TextBox_Partial.cpp`). One reused for the next cell opened at the last cell's height, and a one-line editor lost the grid's Height a moment later (run 44783975649). Focus needs the box in the tree, not loaded (`CUIElement::IsFocusable`), so the new box takes focus as it opens and keys typed right after F2 or Tab reach it. - Enter commits and stays. Tab and Shift+Tab commit and edit the next cell to the right or left, as NSTableView does. Escape cancels. Ctrl+Enter commits a value with line breaks. - Losing focus commits, and a refused commit then cancels. Focus that moves to the TextBox's own menus does not. Unloading the grid and setting `FrozenColumnCount` do the same. - On those paths the editor's ToolTip closes with it, so the host shows a refused commit's Error itself. diff --git a/windows/src/TablePro.DataGrid/DataGridView.Editing.cs b/windows/src/TablePro.DataGrid/DataGridView.Editing.cs index fd3604815f..903678c554 100644 --- a/windows/src/TablePro.DataGrid/DataGridView.Editing.cs +++ b/windows/src/TablePro.DataGrid/DataGridView.Editing.cs @@ -364,14 +364,18 @@ private void OpenEditor(int row, int column, string text) _editText = EditorText(editor); AutomationProperties.SetName(editor, _names[column]); PlaceEditor(); + // Focus needs the box in the tree, not loaded, so keys typed right after F2 or Tab reach it, not the grid. + editor.ApplyTemplate(); + FocusEditor(editor); } - // The editor takes focus once it is in the tree; a box whose edit has already ended does not. + // A grid out of the tree could not focus the box; selecting again here would select over typed text. private void OnEditorLoaded(object sender, RoutedEventArgs e) { - if (IsEditing && ReferenceEquals(sender, _editor)) + if (IsEditing && sender is TextBox editor && ReferenceEquals(editor, _editor) + && editor.FocusState == FocusState.Unfocused) { - FocusEditor((TextBox)sender); + FocusEditor(editor); } } diff --git a/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs b/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs index d0948c61d4..e75f18e225 100644 --- a/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs +++ b/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs @@ -43,6 +43,26 @@ public Task F2OpensATextBoxOverTheCellWithTheHostsText() => WinUiHost.RunAsync(a grid.CancelEdit(); }); + // Keys typed right after F2 or Tab go to whatever has focus, before the new box has loaded. + [Fact] + public Task TheEditorHasFocusAsItOpensAndKeepsWhatIsTypedBeforeItLoads() => WinUiHost.RunAsync(async () => + { + var grid = await ShowEditableAsync(new SyntheticData()); + grid.SelectCell(4, 1, extend: false); + + grid.BeginEdit(); + + var editor = grid.Editor!; + Assert.NotEqual(FocusState.Unfocused, editor.FocusState); + Assert.Equal("r4c1", editor.SelectedText); + editor.SelectedText = "typed"; + await SettleAsync(grid); + Assert.Equal("typed", editor.Text); + Assert.Equal("", editor.SelectedText); + Assert.NotEqual(FocusState.Unfocused, editor.FocusState); + grid.CancelEdit(); + }); + [Fact] public Task TheEditorCoversAWrappedRowAndWrapsItsTextAndWithWrapOffIsOneRowAgain() => WinUiHost.RunAsync(async () => { From c4be2edc389cdb691ec6c41d76cfbd495a1ab9be Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Sat, 21 Nov 2026 00:57:15 +0700 Subject: [PATCH 3/3] test(windows-datagrid): check the caret, not SelectedText, after the editor loads --- windows/tests/TablePro.DataGrid.Tests/EditingTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs b/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs index e75f18e225..9fb6a74800 100644 --- a/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs +++ b/windows/tests/TablePro.DataGrid.Tests/EditingTests.cs @@ -56,9 +56,10 @@ public Task TheEditorHasFocusAsItOpensAndKeepsWhatIsTypedBeforeItLoads() => WinU Assert.NotEqual(FocusState.Unfocused, editor.FocusState); Assert.Equal("r4c1", editor.SelectedText); editor.SelectedText = "typed"; + editor.Select(editor.Text.Length, 0); await SettleAsync(grid); Assert.Equal("typed", editor.Text); - Assert.Equal("", editor.SelectedText); + Assert.Equal((5, 0), (editor.SelectionStart, editor.SelectionLength)); Assert.NotEqual(FocusState.Unfocused, editor.FocusState); grid.CancelEdit(); });