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 f642447 commit fc7ab11
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
20 changes: 9 additions & 11 deletions src/Controls/src/Core/ContentView/ContentView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
62 changes: 58 additions & 4 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 All @@ -346,7 +374,7 @@ public void ContentDoesGetBindingContext()
}

[Fact]
public void ContentParentIsNotInsideTempalte()
public void ContentParentIsNotInsideTemplate()
{
var contentView = new ContentView();
var child = new View();
Expand Down Expand Up @@ -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<SimpleTemplate>().Single();
contentView.Content = new Label();

Assert.Same(bc, simpleTemplate1.BindingContext);

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

var simpleTemplate2 = contentView.GetVisualTreeDescendants().OfType<SimpleTemplate>().Single();

Assert.NotSame(simpleTemplate1, simpleTemplate2);
Assert.Same(bc, simpleTemplate2.BindingContext);
}
}
}

0 comments on commit fc7ab11

Please sign in to comment.