Bug 39369 - Data binding is not listening to property changed events in my ViewModel for OneWay binding.
Summary: Data binding is not listening to property changed events in my ViewModel for ...
Status: RESOLVED INVALID
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.0.0
Hardware: Macintosh Mac OS
: --- major
Target Milestone: ---
Assignee: Bugzilla
URL: http://forums.xamarin.com/discussion/...
Depends on:
Blocks:
 
Reported: 2016-03-04 16:52 UTC by Shawn Castrianni
Modified: 2016-04-04 23:56 UTC (History)
4 users (show)

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


Attachments
Example code (21.96 KB, application/octet-stream)
2016-03-04 17:24 UTC, Shawn Castrianni
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 INVALID

Description Shawn Castrianni 2016-03-04 16:52:12 UTC
All necessary information is in this forum post:

http://forums.xamarin.com/discussion/comment/183045
Comment 1 Shawn Castrianni 2016-03-04 17:23:32 UTC
The attached example code shows the same ListView, one with a OneWay BindableProperty on IsSelected and the other with a TwoWay BindableProperty on IsSelected.

The TwoWay works perfectly allowing the user to toggle on and off each list item.  However, OneWay does not work correctly.  After the very first toggle, no more property changed notifications are received so the UI never changes when toggling on and off list items.

I should NOT have to make a TwoWay BindableProperty just to get property changed notifications to work.  Changes from ViewModel to View IS the OneWay.
Comment 2 Shawn Castrianni 2016-03-04 17:24:17 UTC
Created attachment 15268 [details]
Example code
Comment 3 Jason Smith [MSFT] 2016-03-10 20:06:58 UTC
Your problem is caused because you tried to set the value of IsVisible in the setter of the property. It is important to remember the binding system does not care about about those setters at all and does not call them. Which to your credit it appears you did know, so the next thing is that assigning a value to something that is one way bound WILL clear its binding which is where you got hosed here.

Here is your fixed cell:

		public static BindableProperty NameProperty = BindableProperty.Create<OneWayCell, string> (x => x.Name,
			string.Empty,
			BindingMode.OneWay,
			propertyChanged: (bindable, oldValue, newValue) => {
				OneWayCell view = (OneWayCell)bindable;
				view._nameL.Text = newValue;
			}
		);
		public static BindableProperty IsSelectedProperty = BindableProperty.Create<OneWayCell, bool> (x => x.IsSelected,
			false,
			BindingMode.OneWay,
			propertyChanged: (bindable, oldValue, newValue) => {
				OneWayCell view = (OneWayCell)bindable;
				view._imageV.IsVisible = newValue;
			}
		);
		public string Name {
			get { return (string)GetValue(NameProperty); }
			set {
				SetValue (NameProperty, value);
				
			}
		}
		public bool IsSelected {
			get { return (bool)GetValue(IsSelectedProperty); }
			set {
				SetValue (IsSelectedProperty, value);
				
			}
		}
Comment 4 Shawn Castrianni 2016-03-10 20:32:40 UTC
Thanks, that fixed my problem.  However, I am not sure I really understand the difference between the propertyChanged: function in the BindableProperty and the setter of the real property on the control.  Either the documentation doesn't do a good job of explaining the difference or I missed it.

Coming from an MvvmCross background, I do not need a BindableProperty at all.  I can bind to any normal C# property directly with MvvmCross.
Comment 5 Shawn Castrianni 2016-04-04 23:56:12 UTC
How do I do a proper TwoWay binding?  propertyChanged: handles the VM changing and updating the UI.  How do I handle the UI change to update the VM?