diff --git a/src/Controls/src/Core/ContentView/ContentView.cs b/src/Controls/src/Core/ContentView/ContentView.cs index b447cabdc1f4..8f7c7e9d6b98 100644 --- a/src/Controls/src/Core/ContentView/ContentView.cs +++ b/src/Controls/src/Core/ContentView/ContentView.cs @@ -22,15 +22,15 @@ protected override void OnBindingContextChanged() { base.OnBindingContextChanged(); - IView content = Content; - - if (content == null && (this as IContentView)?.PresentedContent is IView presentedContent) - content = presentedContent; - - ControlTemplate controlTemplate = ControlTemplate; + if (Content is View content) + { + SetInheritedBindingContext(content, BindingContext); + } + } - if (content is BindableObject bindableContent && controlTemplate != null) - SetInheritedBindingContext(bindableContent, BindingContext); + internal override void SetChildInheritedBindingContext(Element child, object context) + { + SetInheritedBindingContext(child, context); } internal override void OnControlTemplateChanged(ControlTemplate oldValue, ControlTemplate newValue) @@ -39,9 +39,7 @@ internal override void OnControlTemplateChanged(ControlTemplate oldValue, Contro return; base.OnControlTemplateChanged(oldValue, newValue); - View content = Content; - ControlTemplate controlTemplate = ControlTemplate; - if (content != null && controlTemplate != null) + if (Content is View content) { SetInheritedBindingContext(content, BindingContext); } diff --git a/src/Controls/tests/Core.UnitTests/ContentViewUnitTest.cs b/src/Controls/tests/Core.UnitTests/ContentViewUnitTest.cs index 1a79a5bfacc6..d025248c9247 100644 --- a/src/Controls/tests/Core.UnitTests/ContentViewUnitTest.cs +++ b/src/Controls/tests/Core.UnitTests/ContentViewUnitTest.cs @@ -315,7 +315,7 @@ public void PacksContent() } [Fact] - public void DoesNotInheritBindingContextToTemplate() + public void DoesInheritBindingContextToTemplate() { var contentView = new ContentView(); var child = new View(); @@ -326,8 +326,36 @@ public void DoesNotInheritBindingContextToTemplate() var bc = "Test"; contentView.BindingContext = bc; - Assert.NotEqual(bc, ((IElementController)contentView).LogicalChildren[0].BindingContext); - Assert.Null(((IElementController)contentView).LogicalChildren[0].BindingContext); + Assert.Equal(bc, ((IElementController)contentView).LogicalChildren[0].BindingContext); + } + + [Fact] + public void DoesntInheritBindingContextToContentFromControlTemplate() + { + var contentView = new ContentView(); + var child1 = new View(); + var child2 = new View(); + + contentView.ControlTemplate = new ControlTemplate(typeof(SimpleTemplate)); + contentView.Content = child1; + + var bc = "Test"; + var bcSimpleTemplate = "other context"; + contentView.BindingContext = bc; + + var simpleTemplate = contentView.GetVisualTreeDescendants().OfType().Single(); + simpleTemplate.BindingContext = bcSimpleTemplate; + + Assert.Equal(bc, child1.BindingContext); + Assert.Equal(contentView.BindingContext, child1.BindingContext); + Assert.Equal(bcSimpleTemplate, simpleTemplate.BindingContext); + + // Change out content and make sure simple templates BC doesn't propagate + contentView.Content = child2; + + Assert.Equal(bc, child2.BindingContext); + Assert.Equal(contentView.BindingContext, child2.BindingContext); + Assert.Equal(bcSimpleTemplate, simpleTemplate.BindingContext); } [Fact] @@ -346,7 +374,7 @@ public void ContentDoesGetBindingContext() } [Fact] - public void ContentParentIsNotInsideTempalte() + public void ContentParentIsNotInsideTemplate() { var contentView = new ContentView(); var child = new View(); @@ -384,5 +412,31 @@ public void ContentView_should_have_the_InternalChildren_correctly_when_Content_ Assert.Single(internalChildren); Assert.Same(expected, internalChildren[0]); } + + + [Fact] + public void BindingContextNotLostWhenSwitchingTemplates() + { + var bc = new object(); + var contentView = new ContentView() + { + BindingContext = bc + }; + + + contentView.ControlTemplate = new ControlTemplate(typeof(SimpleTemplate)); + + var simpleTemplate1 = contentView.GetVisualTreeDescendants().OfType().Single(); + contentView.Content = new Label(); + + Assert.Same(bc, simpleTemplate1.BindingContext); + + contentView.ControlTemplate = new ControlTemplate(typeof(SimpleTemplate)); + + var simpleTemplate2 = contentView.GetVisualTreeDescendants().OfType().Single(); + + Assert.NotSame(simpleTemplate1, simpleTemplate2); + Assert.Same(bc, simpleTemplate2.BindingContext); + } } }