Bug 35086 - One Way Binding does not work on bindable properties
Summary: One Way Binding does not work on bindable properties
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.5.2
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2015-10-20 08:49 UTC by FieldstrikeMobile
Modified: 2015-10-27 06:01 UTC (History)
4 users (show)

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


Attachments
DefaultBindingMode: Two Way... Works (6.55 MB, application/octet-stream)
2015-10-20 08:49 UTC, FieldstrikeMobile
Details
defaultbindingmode: One way which doesnt work (6.55 MB, application/octet-stream)
2015-10-20 08:51 UTC, FieldstrikeMobile
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 FIXED

Description FieldstrikeMobile 2015-10-20 08:49:11 UTC
Created attachment 13449 [details]
DefaultBindingMode: Two Way... Works

I have made a sample where I have extended boxview to include a custom bindableproperty. IsBoxViewRed

Now this property is bound in the xaml to a boolean property on my vm

with a property changed in the Extendedboxview such that if IsBoxViewRed = true the background colour is red and if false the background colour is white.

I press the button to set the boolean property to true, boxview turns red... Brilliant!


I press the other button to set the boolean to false (Thinking it would turn the boxview back white)... but nothing.

Property has changed so that its false but bindable property didnt listen to the change.

Setting defaultbindingmode = bindingmode.twoway works but this is not a fix as I dont want the view relaying that boolean down to the VM. Its oneway for a reason.

(only tested on android currently)
Comment 1 FieldstrikeMobile 2015-10-20 08:51:27 UTC
Created attachment 13450 [details]
defaultbindingmode: One way which doesnt work
Comment 2 Stephane Delcroix 2015-10-26 15:49:51 UTC
The way you define your BindindableProperty, and the Property for it is slightly wrong, in the sense that it relies on loops for being set correctly. that's why it works on 2Way and not on OneWay.

So, instead of 

        public static BindableProperty IAmRedProperty = BindableProperty.Create<RedBoxView, bool>(x => x.IAmRed, false,
        propertyChanged: (b, o, n) =>
        {
            ((RedBoxView)b).IAmRed = n;
        }
        );

        public bool IAmRed
        {
            get { return (bool)this.GetValue(IAmRedProperty); }
            set
            {
                this.SetValue(IAmRedProperty, value);
                if (IAmRed)
                {
                    this.BackgroundColor = Color.Red;
                }
                else
                {
                    this.BackgroundColor = Color.White;
                }
            }
        }

I'd write

        public static BindableProperty IAmRedProperty = BindableProperty.Create<RedBoxView, bool>(x => x.IAmRed, false,
        propertyChanged: (b, o, n) =>
        {
			((RedBoxView)b).BackgroundColor = n ? Color.Red : Color.White;
        }
        );

        public bool IAmRed
        {
            get { return (bool)this.GetValue(IAmRedProperty); }
            set { this.SetValue(IAmRedProperty, value); }
        }

After that, all works as expected
Comment 3 FieldstrikeMobile 2015-10-27 06:01:45 UTC
Thanks that does work. I'm going to read up on Bindable properties in Charles Petzold's book as looking at your code and my code its not 100% clear why one works and the other doesn't but I'm sure it will become clear.

Thanks