Bug 53297 - BindingContext become null after change control template
Summary: BindingContext become null after change control template
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jimmy [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-03-14 05:39 UTC by Jesse Jiang
Modified: 2017-03-20 23:27 UTC (History)
2 users (show)

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


Attachments
sample project (28.29 KB, application/zip)
2017-03-20 23:26 UTC, Jimmy [MSFT]
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 Jesse Jiang 2017-03-14 05:39:26 UTC
Reproduce steps,
1. Create a page without apply any control template.
2. Binding this page to a PageModel
3. Change control template for the main view in runtime, like in some button click event.
4. the mainview's binding context become null
Comment 1 Jimmy [MSFT] 2017-03-14 16:39:12 UTC
Thank you for filing this report! Let me try reproducing this issue with a sample project and I will update the report with my results.
Comment 2 Jimmy [MSFT] 2017-03-20 23:26:55 UTC
Created attachment 20476 [details]
sample project

I believe I was able to reproduce this issue with the sample project I am attaching. In the sample project, I set the BindingContext of the page to a TestViewModel and added a Label with its Text property bound to a property in the view model. When I added a ControlTemplate to the page, the Label Text would disappear.

However, I think this is an expected result as control templates should be using TemplateBinding[1] instead. This means that when you add a control template to the view, the existing bindings in the view should be changed to TemplateBindings so they work properly. As mentioned in the guide linked below, a TemplateBinding's source is set to the parent of the control template, therefore you may not be able to access the properties in the view model unless you map those properties on the parent view as well. A quick sample demonstrating can be check out here: https://www.dropbox.com/s/8i768abgacpi0on/TemplateBinding.zip?dl=0

Hope this helps! If this is not the issue you are experiencing, please re-open this report and attach a sample project that reproduces the issue you are describing. Thanks!

[1] https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/templates/control-templates/template-binding/