Browse Source

Make DataTemplate-created controls namescopes.

pull/464/head
Steven Kirk 11 years ago
parent
commit
79843ae311
  1. 2
      src/Perspex.Controls/Templates/DataTemplateExtensions.cs
  2. 42
      tests/Perspex.Controls.UnitTests/ItemsControlTests.cs
  3. 26
      tests/Perspex.Controls.UnitTests/Presenters/ContentPresenterTests.cs

2
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;
}
}

42
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)

26
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<TextBlock>(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<TextBlock>(target.Child);
Assert.NotNull(NameScope.GetNameScope((Control)target.Child));
}
[Fact]
public void Should_Set_Childs_Parent_To_TemplatedParent()
{

Loading…
Cancel
Save