From 9fe9f5386aa404c529e4c058466b986097a1a7ec Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 22 Nov 2024 13:28:09 -0600 Subject: [PATCH] propagate --- Directory.Build.props | 2 +- src/Controls/src/Core/ContentPresenter.cs | 1 + .../src/Core/ContentView/ContentView.cs | 29 ++++++++++------- .../Core.UnitTests/ContentViewUnitTest.cs | 31 ++++++++++++++++++- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 409a1aeeef8a..beca9e8f64be 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -3,7 +3,7 @@ - true + false portable true Latest diff --git a/src/Controls/src/Core/ContentPresenter.cs b/src/Controls/src/Core/ContentPresenter.cs index 590a0802e913..fea010a8f1c4 100644 --- a/src/Controls/src/Core/ContentPresenter.cs +++ b/src/Controls/src/Core/ContentPresenter.cs @@ -86,6 +86,7 @@ internal override void ComputeConstraintForView(View view) internal override void SetChildInheritedBindingContext(Element child, object context) { + base.SetChildInheritedBindingContext(child, context); // We never want to use the standard inheritance mechanism, we will get this set by our parent } diff --git a/src/Controls/src/Core/ContentView/ContentView.cs b/src/Controls/src/Core/ContentView/ContentView.cs index b447cabdc1f4..fcf00a7c671f 100644 --- a/src/Controls/src/Core/ContentView/ContentView.cs +++ b/src/Controls/src/Core/ContentView/ContentView.cs @@ -22,15 +22,20 @@ protected override void OnBindingContextChanged() { base.OnBindingContextChanged(); - IView content = Content; + //IView content = Content; - if (content == null && (this as IContentView)?.PresentedContent is IView presentedContent) - content = presentedContent; + //if (content == null && (this as IContentView)?.PresentedContent is IView presentedContent) + // content = presentedContent; - ControlTemplate controlTemplate = ControlTemplate; + //ControlTemplate controlTemplate = ControlTemplate; - if (content is BindableObject bindableContent && controlTemplate != null) - SetInheritedBindingContext(bindableContent, 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,12 +44,12 @@ internal override void OnControlTemplateChanged(ControlTemplate oldValue, Contro return; base.OnControlTemplateChanged(oldValue, newValue); - View content = Content; - ControlTemplate controlTemplate = ControlTemplate; - if (content != null && controlTemplate != null) - { - SetInheritedBindingContext(content, BindingContext); - } + //View content = Content; + //ControlTemplate controlTemplate = ControlTemplate; + //if (content != null && controlTemplate != null) + //{ + // SetInheritedBindingContext(content, BindingContext); + //} } object IContentView.Content => Content; diff --git a/src/Controls/tests/Core.UnitTests/ContentViewUnitTest.cs b/src/Controls/tests/Core.UnitTests/ContentViewUnitTest.cs index 1a79a5bfacc6..eb32c9ef9fc5 100644 --- a/src/Controls/tests/Core.UnitTests/ContentViewUnitTest.cs +++ b/src/Controls/tests/Core.UnitTests/ContentViewUnitTest.cs @@ -346,7 +346,7 @@ public void ContentDoesGetBindingContext() } [Fact] - public void ContentParentIsNotInsideTempalte() + public void ContentParentIsNotInsideTemplate() { var contentView = new ContentView(); var child = new View(); @@ -384,5 +384,34 @@ public void ContentView_should_have_the_InternalChildren_correctly_when_Content_ Assert.Single(internalChildren); Assert.Same(expected, internalChildren[0]); } + + + [Fact] + public void BindingContextNotLost() + { + var bc = new object(); + var contentView = new ContentView() + { + }; + + + contentView.ControlTemplate = new ControlTemplate(typeof(SimpleTemplate)); + + var simpleTemplate1 = contentView.GetVisualTreeDescendants().OfType().Single(); + contentView.Content = new Label(); + + contentView.BindingContext = bc; + + var asdf = contentView.Content.BindingContext; + + 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); + } } }