Browse Source

Merge pull request #10273 from jp2masa/issue-10226

Ensure templated parent control theme is applied
pull/10431/head
Steven Kirk 3 years ago
committed by GitHub
parent
commit
b8b6324d63
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Avalonia.Base/Layout/Layoutable.cs
  2. 2
      src/Avalonia.Base/StyledElement.cs
  3. 40
      tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs

6
src/Avalonia.Base/Layout/Layoutable.cs

@ -798,6 +798,12 @@ namespace Avalonia.Layout
InvalidateMeasure(); InvalidateMeasure();
} }
internal override void OnTemplatedParentControlThemeChanged()
{
base.OnTemplatedParentControlThemeChanged();
InvalidateMeasure();
}
/// <summary> /// <summary>
/// Called when the layout manager raises a LayoutUpdated event. /// Called when the layout manager raises a LayoutUpdated event.
/// </summary> /// </summary>

2
src/Avalonia.Base/StyledElement.cs

@ -374,7 +374,7 @@ namespace Avalonia
/// </returns> /// </returns>
public bool ApplyStyling() public bool ApplyStyling()
{ {
if (_initCount == 0 && (!_stylesApplied || !_themeApplied)) if (_initCount == 0 && (!_stylesApplied || !_themeApplied || !_templatedParentThemeApplied))
{ {
GetValueStore().BeginStyling(); GetValueStore().BeginStyling();

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

@ -104,7 +104,7 @@ public class StyledElementTests_Theming
target.Theme = null; target.Theme = null;
Assert.Equal("style", target.Tag); Assert.Equal("style", target.Tag);
} }
[Fact] [Fact]
public void TemplatedParent_Theme_Is_Detached_From_Template_Controls_When_Theme_Property_Cleared() public void TemplatedParent_Theme_Is_Detached_From_Template_Controls_When_Theme_Property_Cleared()
{ {
@ -539,12 +539,42 @@ public class StyledElementTests_Theming
Assert.Same(target.Theme, theme3); Assert.Same(target.Theme, theme3);
} }
[Fact]
public void TemplatedParent_Theme_Change_Applies_To_Children()
{
var theme = CreateDerivedTheme();
var target = CreateTarget();
Assert.Null(target.Theme);
Assert.Null(target.Template);
var root = CreateRoot(target, theme.BasedOn);
Assert.NotNull(target.Theme);
Assert.NotNull(target.Template);
root.Styles.Add(new Style(x => x.OfType<ThemedControl>().Class("foo"))
{
Setters = { new Setter(StyledElement.ThemeProperty, theme) }
});
root.LayoutManager.ExecuteLayoutPass();
var border = Assert.IsType<Border>(target.VisualChild);
Assert.Equal(Brushes.Red, border.Background);
target.Classes.Add("foo");
root.LayoutManager.ExecuteLayoutPass();
Assert.Equal(Brushes.Green, border.Background);
}
private static ThemedControl CreateTarget() private static ThemedControl CreateTarget()
{ {
return new ThemedControl(); return new ThemedControl();
} }
private static TestRoot CreateRoot(Control child) private static TestRoot CreateRoot(Control child, ControlTheme? theme = null)
{ {
var result = new TestRoot() var result = new TestRoot()
{ {
@ -552,7 +582,7 @@ public class StyledElementTests_Theming
{ {
new Style(x => x.OfType<ThemedControl>()) new Style(x => x.OfType<ThemedControl>())
{ {
Setters = { new Setter(StyledElement.ThemeProperty, CreateTheme()) } Setters = { new Setter(StyledElement.ThemeProperty, theme ?? CreateTheme()) }
} }
} }
}; };
@ -580,8 +610,8 @@ public class StyledElementTests_Theming
{ {
new Style(x => x.Nesting().Template().OfType<Border>()) new Style(x => x.Nesting().Template().OfType<Border>())
{ {
Setters = Setters =
{ {
new Setter(Border.BackgroundProperty, Brushes.Red), new Setter(Border.BackgroundProperty, Brushes.Red),
new Setter(Control.TagProperty, tag), new Setter(Control.TagProperty, tag),
} }

Loading…
Cancel
Save