Skip to content

Commit

Permalink
Merge pull request #215 from unoplatform/mergify/bp/unorel/winui/7.1.…
Browse files Browse the repository at this point in the history
…200/pr-214

fix(DataGrid): tabbing doesnt properly bringing cell into view, when cell expanded in edit-mode (backport #214)
  • Loading branch information
jeromelaban authored Apr 25, 2024
2 parents 9756d8c + d6da133 commit 9bdfedb
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions CommunityToolkit.WinUI.UI.Controls.DataGrid/DataGrid/DataGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ private enum ScrollBarsSeparatorVisualState
private ValidationSummaryItem _selectedValidationSummaryItem;
#endif

private DateTime _scrollAdjustmentTimeWindow = DateTime.MinValue;
private FrameworkElement _scrollAdjustmentTarget;

// An approximation of the sum of the heights in pixels of the scrolling rows preceding
// the first displayed scrolling row. Since the scrolled off rows are discarded, the grid
// does not know their actual height. The heights used for the approximation are the ones
Expand Down Expand Up @@ -5921,6 +5924,49 @@ private void EditingElement_Loaded(object sender, RoutedEventArgs e)
PreparingCellForEditPrivate(element);
}

private void EditingElement_SizeChanged(object sender, SizeChangedEventArgs args)
{
if (sender is not FrameworkElement element)
{
return;
}

if (_scrollAdjustmentTarget != element ||
_scrollAdjustmentTimeWindow < DateTime.Now)
{
element.SizeChanged -= EditingElement_SizeChanged;
return;
}

// Workaround for https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4983
// Scroll-into-view is based the cell's size in normal mode. For cell that expands in edit-mode,
// the scroll h/v offsets could be off. So we scroll again using with newly updated size.
if (_editingColumnIndex != -1 &&
EditingRow?.Cells[_editingColumnIndex] is { Content: FrameworkElement editor } cell &&
editor == element)
{
var shouldScroll = new
{
Horizontally = args.PreviousSize.Width < args.NewSize.Width,
Vertically = args.PreviousSize.Height < args.NewSize.Height,
};
if (shouldScroll.Horizontally || shouldScroll.Vertically)
{
ComputeScrollBarsLayout();
}

if (shouldScroll.Horizontally)
{
ScrollColumnIntoView(cell.ColumnIndex);
}

if (shouldScroll.Vertically)
{
ScrollSlotIntoView(cell.RowIndex, scrolledHorizontally: false);
}
}
}

private bool EndCellEdit(DataGridEditAction editAction, bool exitEditingMode, bool keepFocus, bool raiseEvents)
{
if (_editingColumnIndex == -1)
Expand Down Expand Up @@ -6707,8 +6753,12 @@ private void PopulateCellContent(
element.SetStyleWithType(dataGridBoundColumn.EditingElementStyle);
}

_scrollAdjustmentTimeWindow = DateTime.Now.AddSeconds(.5);
_scrollAdjustmentTarget = element;

// Subscribe to the new element's events
element.Loaded += new RoutedEventHandler(EditingElement_Loaded);
element.SizeChanged += new SizeChangedEventHandler(EditingElement_SizeChanged);
}
}
else
Expand Down

0 comments on commit 9bdfedb

Please sign in to comment.