Bug 60172 - TemplateBinding not working on Control with BindingContext set by parent
Summary: TemplateBinding not working on Control with BindingContext set by parent
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.4.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-10-13 08:25 UTC by Jasper Callens
Modified: 2017-10-19 18:08 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

screenshot (38.98 KB, image/png)
2017-10-13 08:25 UTC, Jasper Callens
Demo-Zip (210.21 KB, application/x-zip-compressed)
2017-10-13 08:27 UTC, Jasper Callens

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jasper Callens 2017-10-13 08:25:03 UTC
Created attachment 25299 [details]

I made a ContentPage which contains two ContentViews that use the same ControlTemplate. The ControlTemplate has a Label that uses TemplateBinding for its Text property. The BindingContext is set on the ContentPage from code behind.

Problem is that the TemplateBinding doesn't work whenever the BindingContext is not directly set on the Control that uses the ControlTemplate. 

I think that the ControlTemplate is unaware of the BindingContext change when I assign my ContentView to its parent (which causes the ContentView to receive the parent BindingContext). When I set the BindingContext manualy, the ControlTemplate is aware of the BindingContext change.

**Sample XAML:**

Both ContentViews receive a BindingContext when assigned to their parent.

The SecondContent in the sample XAML sets the BindingContext manualy and makes the TemplateBinding work. 

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                <Style TargetType="Label">
                    <Setter Property="TextColor" Value="White"/>
                <ControlTemplate x:Key="DemoControlTemplate">
                        <Label Text="{TemplateBinding BindingContext.Status}" />
                        <ContentPresenter />
            <ContentView BackgroundColor="Green">
                <ContentView BindingContext="{Binding BindingContext, Source={x:Reference DemoPage}}" 
                             ControlTemplate="{StaticResource DemoControlTemplate}">
                    <Label Text="{Binding Text}" />
            <ContentView BackgroundColor="Red">
                <ContentView x:Name="SecondContent"  ControlTemplate="{StaticResource DemoControlTemplate}">
                    <Label Text="{Binding Text}" />

**Code behind**

    public partial class MainPage : ContentPage
        public MainPage()
            this.BindingContext = new DemoViewModel()
                Status = "StatusDemo",
                Text = "TestDemo",


Result image : 

I was wondering if this is the expected/correct behaviour for TemplateBindings or if it's a Xamarin Forms bug?

XF Version:
Comment 1 Jasper Callens 2017-10-13 08:27:13 UTC
Created attachment 25300 [details]
Comment 2 Paul DiPietro [MSFT] 2017-10-19 18:08:29 UTC
This will work if you bind to Parent.BindingContext.Status on the ControlTemplate.