[forms-devel] How to create a IEnumerable BindableProperty

Jesse Jiang jesse at xam-consulting.com
Tue Oct 4 23:07:57 UTC 2016


Hello,

I try to binding a property like this

public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(BindablePicker), null, BindingMode.OneWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnItemsSourceChanged), null, null, null);

          public IEnumerable ItemsSource
        {
            get { return (IEnumerable)base.GetValue(BindablePicker.ItemsSourceProperty); }
            set { base.SetValue(BindablePicker.ItemsSourceProperty, value); }
        }

But we found that, it won’t call BindablePicker.OnItemsSourceChanged function when the property was changed in BindingContext.

I linked with Xamarin.Forms source code and found that it will call this function when I’m changing the ItemSource in BindingContext,
		internal void Unapply()
		{
			object sourceObject;
			if (_weakSource != null && _weakSource.TryGetTarget(out sourceObject))
			{
				for (var i = 0; i < _parts.Count - 1; i++)
				{
					BindingExpressionPart part = _parts[i];

					if (!part.IsSelf)
					{
						part.TryGetValue(sourceObject, out sourceObject);
					}

					var inpc = sourceObject as INotifyPropertyChanged;
					if (inpc != null)
						inpc.PropertyChanged -= part.ChangeHandler;
				}
			}

			_weakSource = null;
			_weakTarget = null;
		} 

But It didn’t add a new delegate to PropertyChanged in BindingContext, so after that we I raised another property change event it won’t be updated. I known there is quick way to fix that, changing BindingMode.OneWay to BindingMode.TwoWay. But it doesn’t make senesce right? Why the normal data type can work, like string, int, but IEnumerable cannot? 

Did I miss something for binding with IEnumerable, I looked into the ListView source code, it’s very complex, is there any easy way to do this?

Thanks,
Jesse Jiang | Senior Developer 

XAM Consulting - Mobile Technology Specialists

www.xam-consulting.com <http://www.xam-consulting.com/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dot.net/pipermail/forms-devel/attachments/20161005/814d9018/attachment.html>


More information about the forms-devel mailing list