Browse Source

Fix NRE in DataGrid

pull/7292/head
Max Katz 4 years ago
parent
commit
af9f27677d
  1. 2
      src/Avalonia.Controls.DataGrid/DataGridColumns.cs
  2. 28
      src/Avalonia.Controls.DataGrid/DataGridRows.cs
  3. 19
      src/Avalonia.Controls.DataGrid/Primitives/DataGridRowsPresenter.cs

2
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;

28
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<ChildIndexChangedEventArgs> _childIndexChanged;
event EventHandler<ChildIndexChangedEventArgs> 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);
}
/// <summary>
/// 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)
{

19
src/Avalonia.Controls.DataGrid/Primitives/DataGridRowsPresenter.cs

@ -19,6 +19,8 @@ namespace Avalonia.Controls.Primitives
/// </summary>
public sealed class DataGridRowsPresenter : Panel, IChildIndexProvider
{
private EventHandler<ChildIndexChangedEventArgs> _childIndexChanged;
public DataGridRowsPresenter()
{
AddHandler(Gestures.ScrollGestureEvent, OnScrollGesture);
@ -46,18 +48,25 @@ namespace Avalonia.Controls.Primitives
event EventHandler<ChildIndexChangedEventArgs> 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));
}
/// <summary>

Loading…
Cancel
Save