Skip to content

Commit

Permalink
Always propagate BC to ControlTEmplate
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Nov 22, 2024
1 parent 9fe9f53 commit 9fd12e7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/Controls/src/Core/ContentPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ 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
}

Expand Down
23 changes: 8 additions & 15 deletions src/Controls/src/Core/ContentView/ContentView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ 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 BindableObject bindableContent && controlTemplate != null)
// SetInheritedBindingContext(bindableContent, BindingContext);
if (Content is View content)
{
SetInheritedBindingContext(content, BindingContext);
}
}

internal override void SetChildInheritedBindingContext(Element child, object context)
Expand All @@ -44,12 +39,10 @@ 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);
//}
if (Content is View content)
{
SetInheritedBindingContext(content, BindingContext);
}
}

object IContentView.Content => Content;
Expand Down
41 changes: 33 additions & 8 deletions src/Controls/tests/Core.UnitTests/ContentViewUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public void PacksContent()
}

[Fact]
public void DoesNotInheritBindingContextToTemplate()
public void DoesInheritBindingContextToTemplate()
{
var contentView = new ContentView();
var child = new View();
Expand All @@ -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<SimpleTemplate>().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]
Expand Down Expand Up @@ -387,11 +415,12 @@ public void ContentView_should_have_the_InternalChildren_correctly_when_Content_


[Fact]
public void BindingContextNotLost()
public void BindingContextNotLostWhenSwitchingTemplates()
{
var bc = new object();
var contentView = new ContentView()
{
BindingContext = bc
};


Expand All @@ -400,10 +429,6 @@ public void BindingContextNotLost()
var simpleTemplate1 = contentView.GetVisualTreeDescendants().OfType<SimpleTemplate>().Single();
contentView.Content = new Label();

contentView.BindingContext = bc;

var asdf = contentView.Content.BindingContext;

Assert.Same(bc, simpleTemplate1.BindingContext);

contentView.ControlTemplate = new ControlTemplate(typeof(SimpleTemplate));
Expand Down

0 comments on commit 9fd12e7

Please sign in to comment.