@@ -253,6 +253,9 @@ private enum ScrollBarsSeparatorVisualState
253
253
private ValidationSummaryItem _selectedValidationSummaryItem ;
254
254
#endif
255
255
256
+ private DateTime _scrollAdjustmentTimeWindow = DateTime . MinValue ;
257
+ private FrameworkElement _scrollAdjustmentTarget ;
258
+
256
259
// An approximation of the sum of the heights in pixels of the scrolling rows preceding
257
260
// the first displayed scrolling row. Since the scrolled off rows are discarded, the grid
258
261
// 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)
5921
5924
PreparingCellForEditPrivate ( element ) ;
5922
5925
}
5923
5926
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
+
5924
5970
private bool EndCellEdit ( DataGridEditAction editAction , bool exitEditingMode , bool keepFocus , bool raiseEvents )
5925
5971
{
5926
5972
if ( _editingColumnIndex == - 1 )
@@ -6707,8 +6753,12 @@ private void PopulateCellContent(
6707
6753
element . SetStyleWithType ( dataGridBoundColumn . EditingElementStyle ) ;
6708
6754
}
6709
6755
6756
+ _scrollAdjustmentTimeWindow = DateTime . Now . AddSeconds ( .5 ) ;
6757
+ _scrollAdjustmentTarget = element ;
6758
+
6710
6759
// Subscribe to the new element's events
6711
6760
element . Loaded += new RoutedEventHandler ( EditingElement_Loaded ) ;
6761
+ element . SizeChanged += new SizeChangedEventHandler ( EditingElement_SizeChanged ) ;
6712
6762
}
6713
6763
}
6714
6764
else
0 commit comments