From 79843ae311d8e1ed80c3565792c7e2866c7ba9a7 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 3 Mar 2016 22:31:16 +0100 Subject: [PATCH] Make DataTemplate-created controls namescopes. --- .../Templates/DataTemplateExtensions.cs | 2 + .../ItemsControlTests.cs | 42 +++++++++++++++++++ .../Presenters/ContentPresenterTests.cs | 26 ++++++++++++ 3 files changed, 70 insertions(+) diff --git a/src/Perspex.Controls/Templates/DataTemplateExtensions.cs b/src/Perspex.Controls/Templates/DataTemplateExtensions.cs index d06111db0d..4eb9b7f848 100644 --- a/src/Perspex.Controls/Templates/DataTemplateExtensions.cs +++ b/src/Perspex.Controls/Templates/DataTemplateExtensions.cs @@ -45,6 +45,8 @@ namespace Perspex.Controls.Templates result = FuncDataTemplate.Default.Build(data); } + NameScope.SetNameScope((Control)result, new NameScope()); + return result; } } diff --git a/tests/Perspex.Controls.UnitTests/ItemsControlTests.cs b/tests/Perspex.Controls.UnitTests/ItemsControlTests.cs index 49aba552fc..be91a12fe5 100644 --- a/tests/Perspex.Controls.UnitTests/ItemsControlTests.cs +++ b/tests/Perspex.Controls.UnitTests/ItemsControlTests.cs @@ -381,6 +381,48 @@ namespace Perspex.Controls.UnitTests Assert.Equal(new[] { "Foo", "Bar" }, text); } + [Fact] + public void Control_Item_Should_Not_Be_NameScope() + { + var items = new object[] + { + new TextBlock(), + }; + + var target = new ItemsControl + { + Template = GetTemplate(), + Items = items, + }; + + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + + var item = target.Presenter.Panel.LogicalChildren[0]; + Assert.Null(NameScope.GetNameScope((TextBlock)item)); + } + + [Fact] + public void DataTemplate_Created_Item_Should_Be_NameScope() + { + var items = new object[] + { + "foo", + }; + + var target = new ItemsControl + { + Template = GetTemplate(), + Items = items, + }; + + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + + var item = target.Presenter.Panel.LogicalChildren[0]; + Assert.NotNull(NameScope.GetNameScope((TextBlock)item)); + } + private class Item { public Item(string value) diff --git a/tests/Perspex.Controls.UnitTests/Presenters/ContentPresenterTests.cs b/tests/Perspex.Controls.UnitTests/Presenters/ContentPresenterTests.cs index 42a490b38a..b5c1b8f4eb 100644 --- a/tests/Perspex.Controls.UnitTests/Presenters/ContentPresenterTests.cs +++ b/tests/Perspex.Controls.UnitTests/Presenters/ContentPresenterTests.cs @@ -51,6 +51,32 @@ namespace Perspex.Controls.UnitTests.Presenters Assert.Equal("Foo", ((TextBlock)target.Child).Text); } + [Fact] + public void Control_Content_Should_Not_Be_NameScope() + { + var target = new ContentPresenter(); + + target.Content = new TextBlock(); + + Assert.Null(target.Child); + target.UpdateChild(); + Assert.IsType(target.Child); + Assert.Null(NameScope.GetNameScope((Control)target.Child)); + } + + [Fact] + public void DataTemplate_Created_Control_Should_Be_NameScope() + { + var target = new ContentPresenter(); + + target.Content = "Foo"; + + Assert.Null(target.Child); + target.UpdateChild(); + Assert.IsType(target.Child); + Assert.NotNull(NameScope.GetNameScope((Control)target.Child)); + } + [Fact] public void Should_Set_Childs_Parent_To_TemplatedParent() {