Always propagate the BC to the ControlTemplate #26072
Open
+66
−24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of Change
With the initial design of Xamarin.Forms the BC would never propagate to the ControlTemplate as we can see from the test
DoesNotInheritBindingContextToTemplate
that you can reference inside this PR.I'm not really clear on the initial reasoning here. I've compared the behavior to a
ContentControl
in WinUI and the DataContext inWinUI
always propagates to the Template and theDataContext
on theContentView
always propagates to theContentPresenter
even if the template changes theDataContext
We changed this behavior in a slightly awkward way in net8.0 #12536 where we are only setting the
ControlTemplate
BC when theContent
isn't set and it only works for the initialControlTemplate
since we didn't copy this logic intoControlTemplateChanged
It's also a bit weird now because the ordering will change how the BC gets assigned. For example,
This will cause the BC to propagate to the
ControlTemplate
but now it won't propagate when you change theControlTemplate
. Which is basically what the issue this is fixing is seeing happen.This PR fully commits in the direction of just setting the
ControlTemplates
binding context always from theContentView
'sBindingContext
Issues Fixed
Fixes #25934
Fixes #22607
Fixes #14919