Bug 37869 - Binding take place for bindable property which has private set.
Summary: Binding take place for bindable property which has private set.
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.0.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-01-21 12:45 UTC by Dharmendar
Modified: 2016-01-25 04:11 UTC (History)
5 users (show)

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

Replication sample which contains the above mentioned case. (293.50 KB, application/zip)
2016-01-21 12:47 UTC, Dharmendar

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 Dharmendar 2016-01-21 12:45:06 UTC
Xamarin forms allow binding a bindanble property which has private setter,

In the attached sample, i have binded the "CustomCollection" a bindable property which has private setter with "BindingCollection" a clr property. I didn't get any compilation error or runtime error for binding read only property.

Please find attached sample which will reproduce the mentioned issue.

In sample button click will display the updated value of the "CustomCollection" property.
Comment 1 Dharmendar 2016-01-21 12:47:24 UTC
Created attachment 14656 [details]
Replication sample which contains the above mentioned case.
Comment 2 Stephane Delcroix 2016-01-21 15:03:25 UTC
Hi Dharmendar,

Binding happens on the BindableProperty, which is public. Getter and Setter are just there for convenience and are not actually used at all in the Binding mechanism.

You can have readonly Bindable Properties, it just require some more code:

	internal static readonly BindablePropertyKey CustomCollectionPropertyKey = 
		BindableProperty.CreateReadOnly<CustomGrid, ObservableCollection<object>> (w => w.CustomCollection, default(ObservableCollection<object>));

	public static readonly BindableProperty CustomCollectionProperty = CustomCollectionPropertyKey.BindableProperty;

	public ObservableCollection<object> CustomCollection {
		get { return (ObservableCollection<object>)GetValue (CustomCollectionProperty); }
		internal set { SetValue (CustomCollectionPropertyKey, value); } 

note that the BindablePropertyKey is internal, and that's what is required to actually SetValue. In your sample, change that to 'private' (and the setter as well) and you'll be fine.

note also that this will prevent setting the property, but won't prevent modifying the ObservableCollection (use a read only collection if that's what you want).

Warm regards,

the xf team