public void GetTemplateChildren_Should_Not_Return_Nested_Template_Controls() { var target = new TestTemplatedControl(); var border1 = new Border { Name = "border1", TemplatedParent = target }; var inner = new TestTemplatedControl { Name = "inner", TemplatedParent = target }; var border2 = new Border { Name = "border2", TemplatedParent = inner }; var border3 = new Border { Name = "border3", TemplatedParent = inner }; var border4 = new Border { Name = "border4", TemplatedParent = target }; var border5 = new Border { Name = "border5", TemplatedParent = null }; target.AddVisualChild(border1); border1.Child = inner; inner.AddVisualChild(border2); inner.AddVisualChild(border3); border3.Child = border4; border4.Child = border5; var result = target.GetTemplateChildren().Select(x => x.Name).ToArray(); Assert.Equal(new[] { "border1", "inner", "border4" }, result); }
public void Nested_Templated_Controls_Have_Correct_TemplatedParent() { var target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => { return(new ContentControl { Template = new FuncControlTemplate(parent => { return new Border { Child = new ContentPresenter { [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty), } }; }), Content = new Decorator { Child = new TextBlock() } }); }), }; target.ApplyTemplate(); var contentControl = target.GetTemplateChildren().OfType <ContentControl>().Single(); contentControl.ApplyTemplate(); var border = contentControl.GetTemplateChildren().OfType <Border>().Single(); var presenter = contentControl.GetTemplateChildren().OfType <ContentPresenter>().Single(); 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); }
public void Nested_Templated_Controls_Have_Correct_TemplatedParent() { var target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => { return new ContentControl { Template = new FuncControlTemplate(parent => { return new Border { Child = new ContentPresenter { [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty).AsBinding(), } }; }), Content = new Decorator { Child = new TextBlock() } }; }), }; target.ApplyTemplate(); var contentControl = target.GetTemplateChildren().OfType<ContentControl>().Single(); contentControl.ApplyTemplate(); var border = contentControl.GetTemplateChildren().OfType<Border>().Single(); var presenter = contentControl.GetTemplateChildren().OfType<ContentPresenter>().Single(); 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); }