Bug 23942 - Cannot bind properties in BindableObjects added to static resources in XAML
Summary: Cannot bind properties in BindableObjects added to static resources in XAML
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.0
Hardware: Macintosh Mac OS
: Normal enhancement
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-10-20 15:46 UTC by luca.barbieri
Modified: 2016-04-12 18:39 UTC (History)
6 users (show)

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

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 FIXED

Description luca.barbieri 2014-10-20 15:46:04 UTC
If one adds an object to XAML ContentPage.Resources or similar that happens to be a BindableObject (but not a VisualElement), one would expect to be able to bind its properties using XAML.

However, this doesn't work, because bindable objects in static resources do not inherit the parent BindingContext, which makes normal binding fail and also do not inherit its name scope, which makes x:Reference fail, so you can't even bind the BindingContext property manually using x:Reference.

The only way to work around this appears to be to add code like this to the class:

protected override void OnBindingContextChanged ()
{
	base.OnBindingContextChanged ();

	foreach (var res in this.Resources) {
		var bindable = res.Value as BindableObject;
		if (bindable != null)
			bindable.BindingContext = this.BindingContext;
	}
}

I think Xamarin.Forms should do something equivalent to this itself.

In case the object is an Element in addition to being a BindableObject, the Parent property should probably be set as well, while right now it is set to null.

The effect of this issue is that there seems to be no way to add a BindableObject that is not a View to a XAML view or page and bind its properties without writing C# code like the workaround code shown above.

My use case for doing this is a custom "Timer" BindableObject with a bindable "Time" property that is bound to the view model, and that itself exposes a "Second" property that indicated the seconds remaining until the Time property is the current time, which is updated every second by the Timer class, and that is then bound in XAML to a Label to display a countdown timer.

This happens with Xamarin.Forms 1.3.0.6262-pre0 (and I would expect all previous versions).
Comment 1 Parmendra Kumar 2014-12-29 05:23:46 UTC
I have tried to reproduce this issue but I am not able to reproduce it. Could you please provide us a sample project, Environment info so that we can reproduce this issue at our end?
Comment 2 Jason Smith [MSFT] 2016-03-18 08:42:52 UTC
Confirmed
Comment 3 Jason Smith [MSFT] 2016-04-12 18:39:12 UTC
Should be fixed in 2.3.0-pre1