Skip to content

Commit

Permalink
Merge pull request #311 from AvaloniaUI/fixes/update-column-header-co…
Browse files Browse the repository at this point in the history
…lumn-index-when-changing-column-orders

If the user modifies the ColumnList ensure the TreeDataColumnHeader.ColumnIndex is kept in sync
  • Loading branch information
grokys authored Oct 14, 2024
2 parents 7765da6 + 5033f1b commit da3f3ab
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public void Realize(IColumns columns, int columnIndex)
newInpc.PropertyChanged += OnModelPropertyChanged;
}

public void UpdateColumnIndex(int columnIndex)
{
ColumnIndex = columnIndex;
}

public void Unrealize()
{
if (_model is INotifyPropertyChanged oldInpc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected override void RealizeElement(Control element, IColumn column, int inde

protected override void UpdateElementIndex(Control element, int oldIndex, int newIndex)
{
((TreeDataGridColumnHeader)element).UpdateColumnIndex(newIndex);
ChildIndexChanged?.Invoke(this, new ChildIndexChangedEventArgs(element, newIndex));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,35 @@ public void Does_Not_Realize_Columns_Outside_Viewport()
Assert.True(double.IsNaN(columns[3].ActualWidth));
}

[AvaloniaFact(Timeout = 10000)]
public void Header_Column_Indexes_Are_Updated_When_Columns_Are_Updated()
{
var (target, items) = CreateTarget(columns: new IColumn<Model>[]
{
new TextColumn<Model, int>("ID", x => x.Id, width: new GridLength(1, GridUnitType.Star)),
new TextColumn<Model, string?>("Title1", x => x.Title, width: new GridLength(1, GridUnitType.Star)),
new TextColumn<Model, string?>("Title2", x => x.Title, width: new GridLength(1, GridUnitType.Star)),
new TextColumn<Model, string?>("Title3", x => x.Title, width: new GridLength(1, GridUnitType.Star)),
});

AssertColumnIndexes(target, 0, 4);

var source =(FlatTreeDataGridSource<Model>)target.Source!;

var movedColumn = source.Columns[1];
source.Columns.Remove(movedColumn);

AssertColumnIndexes(target, 0, 3);

source.Columns.Add(movedColumn);

var root = (TestWindow)target.GetVisualRoot()!;
root.UpdateLayout();
Dispatcher.UIThread.RunJobs();

AssertColumnIndexes(target, 0, 4);
}

[AvaloniaFact(Timeout = 10000)]
public void Columns_Are_Correctly_Sized_After_Changing_Source()
{
Expand Down

0 comments on commit da3f3ab

Please sign in to comment.