Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Custom ItemsSource bindable property

$
0
0

I'm working on a control for my application that I'd like to have an ItemsSource bindable property that behaves like the stock ListView. I have a backing property of ObservableCollection but am not seeing the bindable property observe when I add or remove elements to that collection.

Here is my bindable property in my custom control (for now just doing simple console writes for POC

public static readonly BindableProperty ItemsSourceProperty =
            BindableProperty.Create (nameof (ItemsSource), typeof (IEnumerable), typeof (MyControl), null,
                                     BindingMode.Default, null, OnItemsSourceChanged);

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

static void OnItemsSourceChanged (BindableObject bindable, object oldvalue, object newvalue)
{
    System.Diagnostics.Debug.WriteLine ("source changed");
}

and the XAML in my view using this control

<local:MyControl ItemsSource="{Binding Models}"

and here is the property that is bound in the VM

ObservableCollection<MyModel> _models;
public ObservableCollection<MyModel> Models
{
    get { return _models; }
    set { SetProperty (ref _models, value); }
}

When I initially set my Models property to a new ObservableCollection, I see the console write in the control. However, when I Add an item to the collection, I don't see that the control is aware of the collection change. I've looked at ListView and ItemsView in the Forms repo but haven't been able to figure out what I'm doing wrong and I think I just need a second pair of eyes...I should note that I'm using the latest Prism library, so their implementation of SetProperty is what I'm using in my VM


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>