Skip to content

Commit 9bdfedb

Browse files
authored
Merge pull request #215 from unoplatform/mergify/bp/unorel/winui/7.1.200/pr-214
fix(DataGrid): tabbing doesnt properly bringing cell into view, when cell expanded in edit-mode (backport #214)
2 parents 9756d8c + d6da133 commit 9bdfedb

File tree

1 file changed

+50
-0
lines changed
  • CommunityToolkit.WinUI.UI.Controls.DataGrid/DataGrid

1 file changed

+50
-0
lines changed

CommunityToolkit.WinUI.UI.Controls.DataGrid/DataGrid/DataGrid.cs

+50
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ private enum ScrollBarsSeparatorVisualState
253253
private ValidationSummaryItem _selectedValidationSummaryItem;
254254
#endif
255255

256+
private DateTime _scrollAdjustmentTimeWindow = DateTime.MinValue;
257+
private FrameworkElement _scrollAdjustmentTarget;
258+
256259
// An approximation of the sum of the heights in pixels of the scrolling rows preceding
257260
// the first displayed scrolling row. Since the scrolled off rows are discarded, the grid
258261
// does not know their actual height. The heights used for the approximation are the ones
@@ -5921,6 +5924,49 @@ private void EditingElement_Loaded(object sender, RoutedEventArgs e)
59215924
PreparingCellForEditPrivate(element);
59225925
}
59235926

5927+
private void EditingElement_SizeChanged(object sender, SizeChangedEventArgs args)
5928+
{
5929+
if (sender is not FrameworkElement element)
5930+
{
5931+
return;
5932+
}
5933+
5934+
if (_scrollAdjustmentTarget != element ||
5935+
_scrollAdjustmentTimeWindow < DateTime.Now)
5936+
{
5937+
element.SizeChanged -= EditingElement_SizeChanged;
5938+
return;
5939+
}
5940+
5941+
// Workaround for https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4983
5942+
// Scroll-into-view is based the cell's size in normal mode. For cell that expands in edit-mode,
5943+
// the scroll h/v offsets could be off. So we scroll again using with newly updated size.
5944+
if (_editingColumnIndex != -1 &&
5945+
EditingRow?.Cells[_editingColumnIndex] is { Content: FrameworkElement editor } cell &&
5946+
editor == element)
5947+
{
5948+
var shouldScroll = new
5949+
{
5950+
Horizontally = args.PreviousSize.Width < args.NewSize.Width,
5951+
Vertically = args.PreviousSize.Height < args.NewSize.Height,
5952+
};
5953+
if (shouldScroll.Horizontally || shouldScroll.Vertically)
5954+
{
5955+
ComputeScrollBarsLayout();
5956+
}
5957+
5958+
if (shouldScroll.Horizontally)
5959+
{
5960+
ScrollColumnIntoView(cell.ColumnIndex);
5961+
}
5962+
5963+
if (shouldScroll.Vertically)
5964+
{
5965+
ScrollSlotIntoView(cell.RowIndex, scrolledHorizontally: false);
5966+
}
5967+
}
5968+
}
5969+
59245970
private bool EndCellEdit(DataGridEditAction editAction, bool exitEditingMode, bool keepFocus, bool raiseEvents)
59255971
{
59265972
if (_editingColumnIndex == -1)
@@ -6707,8 +6753,12 @@ private void PopulateCellContent(
67076753
element.SetStyleWithType(dataGridBoundColumn.EditingElementStyle);
67086754
}
67096755

6756+
_scrollAdjustmentTimeWindow = DateTime.Now.AddSeconds(.5);
6757+
_scrollAdjustmentTarget = element;
6758+
67106759
// Subscribe to the new element's events
67116760
element.Loaded += new RoutedEventHandler(EditingElement_Loaded);
6761+
element.SizeChanged += new SizeChangedEventHandler(EditingElement_SizeChanged);
67126762
}
67136763
}
67146764
else

0 commit comments

Comments
 (0)