diff --git a/src/Perspex.Controls/Control.cs b/src/Perspex.Controls/Control.cs index e568b37244..c4b974fe7e 100644 --- a/src/Perspex.Controls/Control.cs +++ b/src/Perspex.Controls/Control.cs @@ -64,7 +64,7 @@ namespace Perspex.Controls /// Defines the property. /// public static readonly PerspexProperty TemplatedParentProperty = - PerspexProperty.Register(nameof(TemplatedParent)); + PerspexProperty.Register(nameof(TemplatedParent), inherits: true); /// /// Event raised when an element wishes to be scrolled into view. diff --git a/src/Perspex.Controls/Primitives/TemplatedControl.cs b/src/Perspex.Controls/Primitives/TemplatedControl.cs index e0bd785e97..b6ce37fa88 100644 --- a/src/Perspex.Controls/Primitives/TemplatedControl.cs +++ b/src/Perspex.Controls/Primitives/TemplatedControl.cs @@ -212,7 +212,8 @@ namespace Perspex.Controls.Primitives var child = Template.Build(this); var nameScope = new NameScope(); NameScope.SetNameScope((Control)child, nameScope); - SetupTemplateControls(child, nameScope); + child.SetValue(TemplatedParentProperty, this); + RegisterNames(child, nameScope); ((ISetLogicalParent)child).SetParent(this); VisualChildren.Add(child); @@ -259,31 +260,22 @@ namespace Perspex.Controls.Primitives } /// - /// Sets the TemplatedParent property for a control created from the control template and - /// applies the templates of nested templated controls. Also adds each control to its name - /// scope if it has a name. + /// Registers each control with its name scope. /// /// The control. /// The name scope. - private void SetupTemplateControls(IControl control, INameScope nameScope) + private void RegisterNames(IControl control, INameScope nameScope) { - // If control.TemplatedParent is null at this point, then the control is our templated - // child so set its TemplatedParent and register it with its name scope. - if (control.TemplatedParent == null) + if (control.Name != null) { - control.SetValue(TemplatedParentProperty, this); - - if (control.Name != null) - { - nameScope.Register(control.Name, control); - } + nameScope.Register(control.Name, control); } - if (!(control is IPresenter && control.TemplatedParent == this)) + if (control.TemplatedParent == this) { foreach (IControl child in control.GetVisualChildren()) { - SetupTemplateControls(child, nameScope); + RegisterNames(child, nameScope); } } } diff --git a/tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs b/tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs index 178a00b5ac..071ba550b0 100644 --- a/tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs +++ b/tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs @@ -230,8 +230,9 @@ namespace Perspex.Controls.UnitTests.Primitives } }; }), - Content = new TextBlock + Content = new Decorator { + Child = new TextBlock() } }; }), @@ -244,11 +245,13 @@ namespace Perspex.Controls.UnitTests.Primitives var border = contentControl.GetTemplateChildren().OfType().Single(); var presenter = contentControl.GetTemplateChildren().OfType().Single(); - var textBlock = (TextBlock)presenter.Content; + var decorator = (Decorator)presenter.Content; + var textBlock = (TextBlock)decorator.Child; Assert.Equal(target, contentControl.TemplatedParent); Assert.Equal(contentControl, border.TemplatedParent); Assert.Equal(contentControl, presenter.TemplatedParent); + Assert.Equal(target, decorator.TemplatedParent); Assert.Equal(target, textBlock.TemplatedParent); }