Skip to content

Commit

Permalink
Fix nth child selector in data grid header (#15482)
Browse files Browse the repository at this point in the history
* Removed index to display index map so that nth child selector can work with current data.

* Iterate to find the index of the column taking visibility into account.

* Don't rely on VisibleColumnCount since it is updated later.

* Don't take visibility into account when evaluating nth-child and nth-last-child.
  • Loading branch information
appel1 committed May 13, 2024
1 parent 3dc92e0 commit e4e22ae
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
8 changes: 0 additions & 8 deletions src/Avalonia.Controls.DataGrid/DataGridColumnCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Avalonia.Controls
{
internal class DataGridColumnCollection : ObservableCollection<DataGridColumn>
{
private readonly Dictionary<int, int> _columnsMap = new Dictionary<int, int>();
private readonly DataGrid _owningGrid;

public DataGridColumnCollection(DataGrid owningGrid)
Expand Down Expand Up @@ -280,12 +279,10 @@ internal void EnsureVisibleEdgedColumnsWidth()
VisibleStarColumnCount = 0;
VisibleEdgedColumnsWidth = 0;
VisibleColumnCount = 0;
_columnsMap.Clear();

for (int columnIndex = 0; columnIndex < ItemsInternal.Count; columnIndex++)
{
var item = ItemsInternal[columnIndex];
_columnsMap[columnIndex] = item.DisplayIndex;
if (item.IsVisible)
{
VisibleColumnCount++;
Expand All @@ -299,11 +296,6 @@ internal void EnsureVisibleEdgedColumnsWidth()
}
}

internal int GetColumnDisplayIndex(int columnIndex)
{
return _columnsMap.TryGetValue(columnIndex, out var displayIndex) ? displayIndex : -1;
}

internal DataGridColumn GetColumnAtDisplayIndex(int displayIndex)
{
if (displayIndex < 0 || displayIndex >= ItemsInternal.Count || displayIndex >= DisplayIndexMap.Count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ internal DataGridRow OwningRow
int IChildIndexProvider.GetChildIndex(ILogical child)
{
return child is DataGridCell cell
? OwningGrid.ColumnsInternal.GetColumnDisplayIndex(cell.ColumnIndex)
? cell.OwningColumn?.DisplayIndex ?? -1
: throw new InvalidOperationException("Invalid cell type");
}

bool IChildIndexProvider.TryGetTotalCount(out int count)
{
count = OwningGrid.ColumnsInternal.VisibleColumnCount;
count = Children.Count - 1; // Adjust for filler column
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ internal DataGrid OwningGrid
int IChildIndexProvider.GetChildIndex(ILogical child)
{
return child is DataGridColumnHeader header
? OwningGrid.ColumnsInternal.GetColumnDisplayIndex(header.ColumnIndex)
? header.OwningColumn?.DisplayIndex ?? -1
: throw new InvalidOperationException("Invalid cell type");
}

bool IChildIndexProvider.TryGetTotalCount(out int count)
{
count = OwningGrid.ColumnsInternal.VisibleColumnCount;
count = Children.Count - 1; // Adjust for filler column
return true;
}

Expand Down

0 comments on commit e4e22ae

Please sign in to comment.