Browse Source

Can apply control theme to derived types.

pull/8263/head
Steven Kirk 4 years ago
parent
commit
95f70143ca
  1. 2
      src/Avalonia.Base/Styling/ControlTheme.cs
  2. 2
      src/Avalonia.Base/Styling/NestingSelector.cs
  3. 25
      tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs

2
src/Avalonia.Base/Styling/ControlTheme.cs

@ -37,7 +37,7 @@ namespace Avalonia.Styling
var result = BasedOn?.TryAttach(target, host) ?? SelectorMatchResult.NeverThisType;
if (HasSettersOrAnimations && target.StyleKey == TargetType)
if (HasSettersOrAnimations && TargetType.IsAssignableFrom(target.StyleKey))
{
Attach(target, null);
result = SelectorMatchResult.AlwaysThisType;

2
src/Avalonia.Base/Styling/NestingSelector.cs

@ -23,7 +23,7 @@ namespace Avalonia.Styling
{
if (theme.TargetType is null)
throw new InvalidOperationException("ControlTheme has no TargetType.");
return control.StyleKey == theme.TargetType ?
return theme.TargetType.IsAssignableFrom(control.StyleKey) ?
SelectorMatch.AlwaysThisType :
SelectorMatch.NeverThisType;
}

25
tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs

@ -34,6 +34,27 @@ public class StyledElementTests_Theming
Assert.Equal(Brushes.Green, border.Background);
}
[Fact]
public void Theme_Is_Applied_To_Derived_Class_When_Attached_To_Logical_Tree()
{
using var app = UnitTestApplication.Start(TestServices.RealStyler);
var target = new DerivedThemedControl
{
Theme = CreateTheme(),
};
Assert.Null(target.Template);
var root = CreateRoot(target);
Assert.NotNull(target.Template);
var border = Assert.IsType<Border>(target.VisualChild);
Assert.Equal(Brushes.Red, border.Background);
target.Classes.Add("foo");
Assert.Equal(Brushes.Green, border.Background);
}
[Fact]
public void Theme_Is_Detached_When_Theme_Property_Cleared()
{
@ -252,4 +273,8 @@ public class StyledElementTests_Theming
{
public IVisual? VisualChild => VisualChildren?.SingleOrDefault();
}
private class DerivedThemedControl : ThemedControl
{
}
}

Loading…
Cancel
Save