Browse Source

Make TemplatedParent inherit.

This means that we only need to set the TemplatedParent on the root of a
control's template, and on presenter children.
Steven Kirk 10 years ago
parent
commit
567f0135ff

+ 1 - 1
src/Perspex.Controls/Control.cs

@@ -64,7 +64,7 @@ namespace Perspex.Controls
         /// Defines the <see cref="TemplatedParent"/> property.
         /// </summary>
         public static readonly PerspexProperty<ITemplatedControl> TemplatedParentProperty =
-            PerspexProperty.Register<Control, ITemplatedControl>(nameof(TemplatedParent));
+            PerspexProperty.Register<Control, ITemplatedControl>(nameof(TemplatedParent), inherits: true);
 
         /// <summary>
         /// Event raised when an element wishes to be scrolled into view.

+ 8 - 16
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
         }
 
         /// <summary>
-        /// 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.
         /// </summary>
         /// <param name="control">The control.</param>
         /// <param name="nameScope">The name scope.</param>
-        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);
                 }
             }
         }

+ 5 - 2
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<Border>().Single();
             var presenter = contentControl.GetTemplateChildren().OfType<ContentPresenter>().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);
         }