diff --git a/src/Avalonia.Controls.DataGrid/DataGridColumns.cs b/src/Avalonia.Controls.DataGrid/DataGridColumns.cs index ea8a121d29..52f0ad7537 100644 --- a/src/Avalonia.Controls.DataGrid/DataGridColumns.cs +++ b/src/Avalonia.Controls.DataGrid/DataGridColumns.cs @@ -444,7 +444,7 @@ namespace Avalonia.Controls // We need to explicitly collapse the cells of the invisible column because layout only goes through // visible ones - ColumnHeaders.InvalidateChildIndex(); + ColumnHeaders?.InvalidateChildIndex(); foreach (var row in GetAllRows()) { row.Cells[updatedColumn.Index].IsVisible = updatedColumn.IsVisible; diff --git a/src/Avalonia.Controls.DataGrid/DataGridRows.cs b/src/Avalonia.Controls.DataGrid/DataGridRows.cs index 52b67b8921..f3afe2c42d 100644 --- a/src/Avalonia.Controls.DataGrid/DataGridRows.cs +++ b/src/Avalonia.Controls.DataGrid/DataGridRows.cs @@ -17,7 +17,7 @@ using System.Linq; namespace Avalonia.Controls { - public partial class DataGrid : IChildIndexProvider + public partial class DataGrid { internal bool AreRowBottomGridLinesRequired @@ -124,26 +124,6 @@ namespace Avalonia.Controls } } - internal EventHandler _childIndexChanged; - - event EventHandler IChildIndexProvider.ChildIndexChanged - { - add => _childIndexChanged += value; - remove => _childIndexChanged -= value; - } - - int IChildIndexProvider.GetChildIndex(ILogical child) - { - return child is DataGridRow row - ? row.Index - : throw new InvalidOperationException("Invalid DataGrid child"); - } - - bool IChildIndexProvider.TryGetTotalCount(out int count) - { - return DataConnection.TryGetCount(false, true, out count); - } - /// /// Clears the entire selection. Displayed rows are deselected explicitly to visualize /// potential transition effects @@ -832,7 +812,7 @@ namespace Avalonia.Controls if (row.Slot > slotDeleted) { CorrectRowAfterDeletion(row, wasRow); - _childIndexChanged?.Invoke(this, new ChildIndexChangedEventArgs(row)); + _rowsPresenter?.InvalidateChildIndex(row); } } @@ -888,7 +868,7 @@ namespace Avalonia.Controls if (row.Slot >= slotInserted) { DataGrid.CorrectRowAfterInsertion(row, rowInserted); - _childIndexChanged?.Invoke(this, new ChildIndexChangedEventArgs(row)); + _rowsPresenter?.InvalidateChildIndex(row); } } @@ -1507,7 +1487,7 @@ namespace Avalonia.Controls if (row.IsRecycled) { row.ApplyCellsState(); - _childIndexChanged?.Invoke(this, new ChildIndexChangedEventArgs(row)); + _rowsPresenter?.InvalidateChildIndex(row); } else if (row == EditingRow) { diff --git a/src/Avalonia.Controls.DataGrid/Primitives/DataGridRowsPresenter.cs b/src/Avalonia.Controls.DataGrid/Primitives/DataGridRowsPresenter.cs index 0b9bd0a564..5d82689eff 100644 --- a/src/Avalonia.Controls.DataGrid/Primitives/DataGridRowsPresenter.cs +++ b/src/Avalonia.Controls.DataGrid/Primitives/DataGridRowsPresenter.cs @@ -19,6 +19,8 @@ namespace Avalonia.Controls.Primitives /// public sealed class DataGridRowsPresenter : Panel, IChildIndexProvider { + private EventHandler _childIndexChanged; + public DataGridRowsPresenter() { AddHandler(Gestures.ScrollGestureEvent, OnScrollGesture); @@ -46,18 +48,25 @@ namespace Avalonia.Controls.Primitives event EventHandler IChildIndexProvider.ChildIndexChanged { - add => OwningGrid._childIndexChanged += value; - remove => OwningGrid._childIndexChanged -= value; + add => _childIndexChanged += value; + remove => _childIndexChanged -= value; + } + + int IChildIndexProvider.GetChildIndex(ILogical child) + { + return child is DataGridRow row + ? row.Index + : throw new InvalidOperationException("Invalid DataGrid child"); } bool IChildIndexProvider.TryGetTotalCount(out int count) { - return ((IChildIndexProvider)OwningGrid).TryGetTotalCount(out count); + return OwningGrid.DataConnection.TryGetCount(false, true, out count); } - int IChildIndexProvider.GetChildIndex(ILogical child) + internal void InvalidateChildIndex(DataGridRow row) { - return ((IChildIndexProvider)OwningGrid).GetChildIndex(child); + _childIndexChanged?.Invoke(this, new ChildIndexChangedEventArgs(row)); } ///