diff --git a/src/Avalonia.Styling/StyledElement.cs b/src/Avalonia.Styling/StyledElement.cs index 8a536485a1..09991f2ece 100644 --- a/src/Avalonia.Styling/StyledElement.cs +++ b/src/Avalonia.Styling/StyledElement.cs @@ -418,12 +418,16 @@ namespace Avalonia var e = new LogicalTreeAttachmentEventArgs(newRoot, this, parent); OnAttachedToLogicalTreeCore(e); } - else + else if (parent is null) { // If we were attached to the logical tree, we piggyback on the tree traversal // there to raise resources changed notifications. If we're being removed from - // the logical tree or attached to a control which is not rooted, then traverse - // the tree raising notifications now. + // the logical tree, then traverse the tree raising notifications now. + // + // We don't raise resources changed notifications if we're being attached to a + // non-rooted control beacuse it's unlikely that dynamic resources need to be + // correct until the control is added to the tree, and it causes a *lot* of + // notifications. NotifyResourcesChanged(); } diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests.cs index 2930098371..58d2eedb1f 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests.cs @@ -536,6 +536,7 @@ namespace Avalonia.Controls.UnitTests.Primitives }), }; + var root = new TestRoot(target); target.ApplyTemplate(); var contentPresenter = Assert.IsType(target.GetVisualChildren().Single()); @@ -565,6 +566,7 @@ namespace Avalonia.Controls.UnitTests.Primitives }), }; + var root = new TestRoot(target); target.ApplyTemplate(); var contentPresenter = Assert.IsType(target.GetVisualChildren().Single()); diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs index 412b5ec083..ed3e4c986b 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs @@ -634,33 +634,36 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions [Fact] public void Control_Property_Is_Updated_When_Parent_Is_Changed() { - var xaml = @" - - + #ff506070 - + -"; +"; - var loader = new AvaloniaXamlLoader(); - var userControl = (UserControl)loader.Load(xaml); - var border = userControl.FindControl("border"); + var loader = new AvaloniaXamlLoader(); + var window = (Window)loader.Load(xaml); + var border = window.FindControl("border"); - DelayedBinding.ApplyBindings(border); + DelayedBinding.ApplyBindings(border); - var brush = (SolidColorBrush)border.Background; - Assert.Equal(0xff506070, brush.Color.ToUint32()); + var brush = (SolidColorBrush)border.Background; + Assert.Equal(0xff506070, brush.Color.ToUint32()); - userControl.Content = null; + window.Content = null; - Assert.Null(border.Background); + Assert.Null(border.Background); - userControl.Content = border; + window.Content = border; - brush = (SolidColorBrush)border.Background; - Assert.Equal(0xff506070, brush.Color.ToUint32()); + brush = (SolidColorBrush)border.Background; + Assert.Equal(0xff506070, brush.Color.ToUint32()); + } } [Fact] diff --git a/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs b/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs index ef5a097f87..fcbccba695 100644 --- a/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs +++ b/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs @@ -530,9 +530,9 @@ namespace Avalonia.Styling.UnitTests } [Fact] - public void Changing_Parent_Raises_ResourcesChanged() + public void Adding_To_Logical_Tree_Raises_ResourcesChanged() { - var target = new TestControl(); + var target = new TestRoot(); var parent = new Decorator { Resources = { { "foo", "bar" } } }; var raised = 0;