diff --git a/src/Avalonia.Controls/Primitives/TemplatedControl.cs b/src/Avalonia.Controls/Primitives/TemplatedControl.cs
index ba4c5027d0..32e220b789 100644
--- a/src/Avalonia.Controls/Primitives/TemplatedControl.cs
+++ b/src/Avalonia.Controls/Primitives/TemplatedControl.cs
@@ -357,7 +357,7 @@ namespace Avalonia.Controls.Primitives
if (control.TemplatedParent == this)
{
- foreach (IControl child in control.GetVisualChildren())
+ foreach (IControl child in control.GetLogicalChildren())
{
RegisterNames(child, nameScope);
}
diff --git a/src/Avalonia.Styling/StyledElement.cs b/src/Avalonia.Styling/StyledElement.cs
index d314a8d44e..ae2cec5561 100644
--- a/src/Avalonia.Styling/StyledElement.cs
+++ b/src/Avalonia.Styling/StyledElement.cs
@@ -677,23 +677,6 @@ namespace Avalonia
if (Name != null)
{
_nameScope?.Register(Name, this);
-
- var visualParent = Parent as StyledElement;
-
- if (this is INameScope && visualParent != null)
- {
- // If we have e.g. a named UserControl in a window then we want that control
- // to be findable by name from the Window, so register with both name scopes.
- // This differs from WPF's behavior in that XAML manually registers controls
- // with name scopes based on the XAML file in which the name attribute appears,
- // but we're trying to avoid XAML magic in Avalonia in order to made code-
- // created UIs easy. This will cause problems if a UserControl declares a name
- // in its XAML and that control is included multiple times in a parent control
- // (as the name will be duplicated), however at the moment I'm fine with saying
- // "don't do that".
- var parentNameScope = NameScope.FindNameScope(visualParent);
- parentNameScope?.Register(Name, this);
- }
}
}
diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs
index 2e67541c1f..743fc82f29 100644
--- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs
+++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs
@@ -197,6 +197,22 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
Assert.Equal("Foo", button.Content);
}
+ [Fact]
+ public void Named_UserControl_Is_Added_To_Parent_NameScope()
+ {
+ using (UnitTestApplication.Start(TestServices.StyledWindow))
+ {
+ var xaml = @"
+
+
+";
+
+ var control = AvaloniaXamlLoader.Parse(xaml);
+
+ Assert.NotNull(control.FindControl("foo"));
+ }
+ }
+
[Fact]
public void Direct_Content_In_ItemsControl_Is_Operational()
{
diff --git a/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs b/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs
index 4970addd81..7fdd70799f 100644
--- a/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs
+++ b/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs
@@ -273,13 +273,10 @@ namespace Avalonia.Styling.UnitTests
var root = new TestRoot();
var child = new Border();
- ((ISupportInitialize)child).BeginInit();
+ child.BeginInit();
root.Child = child;
child.Name = "foo";
- Assert.Null(root.FindControl("foo"));
- ((ISupportInitialize)child).EndInit();
-
- Assert.Same(root.FindControl("foo"), child);
+ child.EndInit();
}
}
diff --git a/tests/Avalonia.Styling.UnitTests/StyledElementTests_NameScope.cs b/tests/Avalonia.Styling.UnitTests/StyledElementTests_NameScope.cs
index 47c540f44a..47c34dfd38 100644
--- a/tests/Avalonia.Styling.UnitTests/StyledElementTests_NameScope.cs
+++ b/tests/Avalonia.Styling.UnitTests/StyledElementTests_NameScope.cs
@@ -1,11 +1,6 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
-using System;
-using Avalonia.Controls.Presenters;
-using Avalonia.Controls.Templates;
-using Avalonia.Rendering;
-using Avalonia.Styling;
using Avalonia.UnitTests;
using Xunit;
@@ -70,23 +65,5 @@ namespace Avalonia.Controls.UnitTests
Assert.Null(NameScope.GetNameScope((StyledElement)root.Presenter).Find("foo"));
}
-
- [Fact]
- public void Control_That_Is_NameScope_Should_Register_With_Parent_NameScope()
- {
- UserControl userControl;
- var root = new TestTemplatedRoot
- {
- Content = userControl = new UserControl
- {
- Name = "foo",
- }
- };
-
- root.ApplyTemplate();
-
- Assert.Same(userControl, root.FindControl("foo"));
- Assert.Same(userControl, userControl.FindControl("foo"));
- }
}
}