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

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


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


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:
Status:
RESOLVED ANSWERED

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

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"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:DemoControlTemplateIssue"
                 x:Class="DemoControlTemplateIssue.MainPage"
                 x:Name="DemoPage">
        <ContentPage.Resources>
            <ResourceDictionary>
                <Style TargetType="Label">
                    <Setter Property="TextColor" Value="White"/>
                </Style>
                <ControlTemplate x:Key="DemoControlTemplate">
                    <StackLayout>
                        <Label Text="{TemplateBinding BindingContext.Status}" />
                        <ContentPresenter />
                    </StackLayout>
                </ControlTemplate>
            </ResourceDictionary>
        </ContentPage.Resources>
    
        <StackLayout>
            <ContentView BackgroundColor="Green">
                <ContentView BindingContext="{Binding BindingContext, Source={x:Reference DemoPage}}" 
                             ControlTemplate="{StaticResource DemoControlTemplate}">
                    <Label Text="{Binding Text}" />
                </ContentView>
            </ContentView>
    
            <ContentView BackgroundColor="Red">
                <ContentView x:Name="SecondContent"  ControlTemplate="{StaticResource DemoControlTemplate}">
                    <Label Text="{Binding Text}" />
                </ContentView>
            </ContentView>
        </StackLayout>
    </ContentPage>

**Code behind**

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

**Result**

Result image : 

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

XF Version: 2.4.0.282
Comment 1 Jasper Callens 2017-10-13 08:27:13 UTC
Created attachment 25300 [details]
Demo-Zip
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.

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/templates/control-templates/template-binding/#Creating_a_TemplateBinding_in_XAML