Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[C] reset the BindingContext on template change #998

Merged
merged 2 commits into from
Jun 21, 2017
Merged

Conversation

StephaneDelcroix
Copy link
Member

@StephaneDelcroix StephaneDelcroix commented Jun 16, 2017

Description of Change

When we remove an old template before setting a new one, all Bindings are unapplied (as part of unparenting).
This fix force a setting back the binding context when the an existing ControlTemplate is swapped for another one.

Bugs Fixed

API Changes

/

Behavioral Changes

/

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Rebased on top of master at time of PR
  • Changes adhere to coding standard
  • Consolidate commits as makes sense

@samhouts
Copy link
Member

Unit tests aren't happy.

@@ -27,5 +27,31 @@ public ExpectedView()
{
}
}

public class MyTemplate : StackLayout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. This formatting is correct but the rest of the file uses spaces..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯_(ツ)_/¯

};
cv.BindingContext = "Foo";

Assume.That(label.Text, Is.EqualTo("Foo"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. So you use an assume to indicate this is not the test but an invariant of the the test itself.

if (oldValue == null)
return;

base.OnControlTemplateChanged(oldValue, newValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not have this before line 28? Is it always the case that the base class will similarly do nothing if the oldValue is null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

safer, in case the base is modified one day

base.OnControlTemplateChanged(oldValue, newValue);
View content = Content;
ControlTemplate controlTemplate = ControlTemplate;
if (content != null && controlTemplate != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the trap for content == null? Seems TemplateUtilities.cs L122 ensures that's the case. Should we throw instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that code is, on purpose, a line-by-line copy of OnBindingContextChanged

@StephaneDelcroix StephaneDelcroix merged commit 28b343b into master Jun 21, 2017
@StephaneDelcroix StephaneDelcroix deleted the fix-bz43685 branch June 21, 2017 08:32
jimmgarrido pushed a commit to jimmgarrido/Xamarin.Forms that referenced this pull request Jun 21, 2017
* [C] reset the BindingContext on template change

* fix test
mattleibow pushed a commit that referenced this pull request Jan 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants