public void Should_Remove_Old_Child_From_LogicalChildren_On_ContentChanged_OutsideTemplate() { var target = new ContentPresenter { ContentTemplate = new FuncDataTemplate <string>(t => new ContentControl() { Content = t }, false) }; target.Content = "foo"; target.UpdateChild(); var foo = target.Child as ContentControl; Assert.NotNull(foo); var logicalChildren = target.GetLogicalChildren(); Assert.Equal(1, logicalChildren.Count()); target.Content = "bar"; target.UpdateChild(); Assert.Equal(null, foo.Parent); logicalChildren = target.GetLogicalChildren(); Assert.Equal(1, logicalChildren.Count()); Assert.NotEqual(foo, logicalChildren.First()); }
public void Assigning_Control_To_Content_After_NonControl_Should_Clear_DataContext() { var target = new ContentPresenter(); target.Content = "foo"; target.UpdateChild(); Assert.True(target.IsSet(Control.DataContextProperty)); target.Content = new Border(); target.UpdateChild(); Assert.False(target.IsSet(Control.DataContextProperty)); }
public void Should_Reset_InheritanceParent_When_Child_Removed() { var logicalParent = new Canvas(); var child = new TextBlock(); var target = new ContentPresenter(); ((ISetLogicalParent)child).SetParent(logicalParent); target.Content = child; target.UpdateChild(); target.Content = null; target.UpdateChild(); // InheritanceParent is exposed via StylingParent. Assert.Same(logicalParent, ((IStyledElement)child).StylingParent); }
public void Content_Alignment_And_Padding_Are_Applied_To_Child_Bounds( HorizontalAlignment h, VerticalAlignment v, double expectedX, double expectedY, double expectedWidth, double expectedHeight) { Border content; var target = new ContentPresenter { HorizontalContentAlignment = h, VerticalContentAlignment = v, Padding = new Thickness(10), Content = content = new Border { MinWidth = 16, MinHeight = 16, }, }; target.UpdateChild(); target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); Assert.Equal(new Rect(expectedX, expectedY, expectedWidth, expectedHeight), content.Bounds); }
public void Replacing_Template_Releases_Events() { var p1 = new ContentPresenter { Name = "Content_1_Presenter" }; var p2 = new ContentPresenter { Name = "Content_2_Presenter" }; var target = new TestControl { Template = new FuncControlTemplate(_ => new Panel { Children = { p1, p2 } }) }; target.ApplyTemplate(); Control tc; p1.Content = tc = new Control(); p1.UpdateChild(); Assert.Contains(tc, target.GetLogicalChildren()); p2.Content = tc = new Control(); p2.UpdateChild(); Assert.Contains(tc, target.GetLogicalChildren()); target.Template = null; p1.Content = tc = new Control(); p1.UpdateChild(); Assert.DoesNotContain(tc, target.GetLogicalChildren()); p2.Content = tc = new Control(); p2.UpdateChild(); Assert.DoesNotContain(tc, target.GetLogicalChildren()); }
public void Should_Raise_DetachedFromLogicalTree_On_Content_Changed_Standalone() { var target = new ContentPresenter { ContentTemplate = new FuncDataTemplate <string>((t, _) => new ContentControl() { Content = t }, false) }; var parentMock = new Mock <Control>(); parentMock.As <IContentPresenterHost>(); parentMock.As <IRenderRoot>(); parentMock.As <ILogicalRoot>(); (target as ISetLogicalParent).SetParent(parentMock.Object); target.Content = "foo"; target.UpdateChild(); var foo = target.Child as ContentControl; bool foodetached = false; Assert.NotNull(foo); Assert.Equal("foo", foo.Content); foo.DetachedFromLogicalTree += delegate { foodetached = true; }; target.Content = "bar"; target.UpdateChild(); var bar = target.Child as ContentControl; Assert.NotNull(bar); Assert.True(bar != foo); Assert.False((foo as IControl).IsAttachedToLogicalTree); Assert.True(foodetached); }
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)); }
public void Setting_Content_To_Control_Should_Set_Child() { var target = new ContentPresenter(); var child = new Border(); target.Content = child; Assert.Null(target.Child); target.UpdateChild(); Assert.Equal(child, target.Child); }
public void Assigning_NonControl_To_Content_Should_Set_DataContext_On_UpdateChild() { var target = new ContentPresenter { Content = "foo", }; target.UpdateChild(); Assert.Equal("foo", target.DataContext); }
public void Setting_Content_To_String_Should_Create_TextBlock() { var target = new ContentPresenter(); target.Content = "Foo"; Assert.Null(target.Child); target.UpdateChild(); Assert.IsType <TextBlock>(target.Child); Assert.Equal("Foo", ((TextBlock)target.Child).Text); }
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)); }
public void Clearing_String_Content_Should_Remove_Child_Immediately() { var target = new ContentPresenter(); target.Content = "Foo"; target.UpdateChild(); Assert.IsType <TextBlock>(target.Child); target.Content = null; Assert.Null(target.Child); }
public void Should_Set_Childs_Parent_To_Itself_Outside_Template() { var content = new Border(); var target = new ContentPresenter { Content = content }; target.UpdateChild(); Assert.Same(target, content.Parent); }
public void Should_Not_Create_Child_When_Content_And_Template_Are_Null() { var target = new ContentPresenter { ContentTemplate = null, Content = null }; target.UpdateChild(); Assert.Null(target.Child); }
public void Clearing_Control_Content_Should_Remove_Child_Immediately() { var target = new ContentPresenter(); var child = new Border(); target.Content = child; target.UpdateChild(); Assert.Equal(child, target.Child); target.Content = null; Assert.Null(target.Child); }
public void Tries_To_Recycle_DataTemplate() { var target = new ContentPresenter { DataTemplates = new DataTemplates { new FuncDataTemplate <string>(_ => new Border(), true), }, Content = "foo", }; target.UpdateChild(); var control = target.Child; Assert.IsType <Border>(control); target.Content = "bar"; target.UpdateChild(); Assert.Same(control, target.Child); }
public void Should_Add_Child_To_Own_LogicalChildren_Standalone() { var content = new Border(); var target = new ContentPresenter { Content = content }; target.UpdateChild(); var logicalChildren = target.GetLogicalChildren(); Assert.Single(logicalChildren); Assert.Equal(content, logicalChildren.First()); }
public void Should_Add_Child_To_Own_LogicalChildren_Outside_Template() { var content = new Border(); var target = new ContentPresenter { Content = content }; target.UpdateChild(); var logicalChildren = target.GetLogicalChildren(); Assert.Equal(1, logicalChildren.Count()); Assert.Equal(content, logicalChildren.First()); }
public void Setting_Content_To_Control_Should_Not_Set_Child_Unless_UpdateChild_Called() { var target = new ContentPresenter(); var child = new Border(); target.Content = child; Assert.Null(target.Child); target.ApplyTemplate(); Assert.Null(target.Child); target.UpdateChild(); Assert.Equal(child, target.Child); }
public void Should_Create_Child_When_Content_Is_Null_And_Expected_NullableValueType_With_FuncDataTemplate() { var target = new ContentPresenter { ContentTemplate = new FuncDataTemplate <int?>(_ => true, (_, __) => new TextBlock { Text = "Hello World" }), Content = null }; target.UpdateChild(); Assert.NotNull(target.Child); }
public void Should_Create_Child_Even_With_Null_Content_When_ContentTemplate_Is_Set() { var target = new ContentPresenter { ContentTemplate = new FuncDataTemplate <object>(_ => true, (_, __) => new TextBlock { Text = "Hello World" }), Content = null }; target.UpdateChild(); var textBlock = Assert.IsType <TextBlock>(target.Child); Assert.Equal("Hello World", textBlock.Text); }
public void Content_Can_Be_Stretched() { Border content; var target = new ContentPresenter { Content = content = new Border { MinWidth = 16, MinHeight = 16, }, }; target.UpdateChild(); target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); Assert.Equal(new Rect(0, 0, 100, 100), content.Bounds); }
public void Should_Not_Create_Child_Even_With_Null_Content_And_DataTemplates_InsteadOf_ContentTemplate() { var target = new ContentPresenter { DataTemplates = { new FuncDataTemplate <object>(_ => true, (_, __) => new TextBlock { Text = "Hello World" }) }, Content = null }; target.UpdateChild(); Assert.Null(target.Child); }
public void Content_Can_Be_Right_Aligned() { Border content; var target = new ContentPresenter { Content = content = new Border { MinWidth = 16, MinHeight = 16, HorizontalAlignment = HorizontalAlignment.Right }, }; target.UpdateChild(); target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); Assert.Equal(new Rect(84, 0, 16, 100), content.Bounds); }
public void Content_Can_Be_Bottom_Aligned() { Border content; var target = new ContentPresenter { Content = content = new Border { MinWidth = 16, MinHeight = 16, VerticalAlignment = VerticalAlignment.Bottom, }, }; target.UpdateChild(); target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); Assert.Equal(new Rect(0, 84, 100, 16), content.Bounds); }
public void Adding_To_Logical_Tree_Should_Reevaluate_DataTemplates() { var target = new ContentPresenter { Content = "Foo", }; target.UpdateChild(); Assert.IsType <TextBlock>(target.Child); var root = new TestRoot { DataTemplates = new DataTemplates { new FuncDataTemplate <string>(x => new Decorator()), }, }; root.Child = target; target.ApplyTemplate(); Assert.IsType <Decorator>(target.Child); }
public void Child_Arrange_With_Zero_Height_When_Padding_Height_Greater_Than_Child_Height() { Border content; var target = new ContentPresenter { Padding = new Thickness(32), MaxHeight = 32, MaxWidth = 32, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, Content = content = new Border { Height = 0, Width = 0, }, }; target.UpdateChild(); target.Arrange(new Rect(0, 0, 100, 100)); Assert.Equal(new Rect(32, 32, 0, 0), content.Bounds); }
public void Should_Correctly_Align_Child_With_Fixed_Size() { Border content; var target = new ContentPresenter { HorizontalContentAlignment = HorizontalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Stretch, Content = content = new Border { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Bottom, Width = 16, Height = 16, }, }; target.UpdateChild(); target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); // Check correct result for Issue #1447. Assert.Equal(new Rect(0, 84, 16, 16), content.Bounds); }
public void Should_Raise_DetachedFromLogicalTree_On_Detached_OutsideTemplate() { var target = new ContentPresenter { ContentTemplate = new FuncDataTemplate <string>(t => new ContentControl() { Content = t }, false) }; var parentMock = new Mock <Control>(); parentMock.As <IContentPresenterHost>(); parentMock.As <IStyleRoot>(); (target as ISetLogicalParent).SetParent(parentMock.Object); target.Content = "foo"; target.UpdateChild(); var foo = target.Child as ContentControl; bool foodetached = false; Assert.NotNull(foo); Assert.Equal("foo", foo.Content); foo.DetachedFromLogicalTree += delegate { foodetached = true; }; (target as ISetLogicalParent).SetParent(null); Assert.False((foo as IControl).IsAttachedToLogicalTree); Assert.True(foodetached); }