Browse Source
fix: Set the LogicalParent of the TableViewColumn (#21840 )
TableViewColumn extends StyledElement. However, many features such as StyleSelectors did not work because no logical parent was set. This is solved by registering the TableView as a logical parent when the TableViewColumn is attached (and setting it to null when detacting).
pull/22043/head
Ole Ross
2 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
44 additions and
0 deletions
src/Avalonia.Controls/TableView.cs
tests/Avalonia.Controls.UnitTests/TableViewTests.cs
@ -5,6 +5,7 @@ using Avalonia.Collections;
using Avalonia.Controls.Presenters ;
using Avalonia.Controls.Templates ;
using Avalonia.Data ;
using Avalonia.LogicalTree ;
using Avalonia.Metadata ;
using static Avalonia . Controls . Presenters . TableViewLayoutHelper ;
@ -139,6 +140,7 @@ public class TableView : ListBox
}
column . TableView = this ;
( ( ISetLogicalParent ) column ) . SetParent ( this ) ;
}
private void DetachColumn ( TableViewColumn column )
@ -146,6 +148,7 @@ public class TableView : ListBox
Debug . Assert ( column . TableView = = this | | column . TableView is null ) ;
column . TableView = null ;
( ( ISetLogicalParent ) column ) . SetParent ( null ) ;
column . ActualWidth = double . NaN ;
}
@ -70,6 +70,47 @@ public sealed class TableViewTests : ScopedTestBase
Assert . Null ( column . TableView ) ;
}
[Fact]
public void Column_LogicalParent_Is_Set_When_Added_And_Unset_When_Removed ( )
{
using var app = Start ( ) ;
var target = CreateTarget ( new [ ] { "Foo" } ) ;
var column = new TableViewColumn ( ) ;
target . Columns . Add ( column ) ;
Prepare ( target ) ;
Assert . Same ( target , column . GetLogicalParent ( ) ) ;
target . Columns . Remove ( column ) ;
Assert . Null ( column . GetLogicalParent ( ) ) ;
}
[Fact]
public void Column_Receives_Styles_From_TableView ( )
{
using var app = Start ( ) ;
var target = CreateTarget ( new [ ] { "Foo" } ) ;
var style = new Style ( x = > x . OfType < TableViewColumn > ( ) . Class ( "right-align" ) )
{
Setters = { new Setter ( TableViewColumn . HorizontalContentAlignmentProperty , HorizontalAlignment . Right ) }
} ;
target . Styles . Add ( style ) ;
var column = new TableViewColumn
{
Classes = { "right-align" }
} ;
target . Columns . Add ( column ) ;
Prepare ( target ) ;
Assert . Equal ( HorizontalAlignment . Right , column . HorizontalContentAlignment ) ;
}
[Fact]
public void Column_Cannot_Belong_To_Two_TableViews ( )
{