0
votes

I have a picker control:

<Picker Title="Number of People" 
            ItemsSource="{Binding SomeList, Source={x:Static local:MyModelHandler.MyModel}}" 
            SelectedItem="{Binding SomeListSelectedIndex, Source={x:Static local:MyModelHandler.MyModel}}">
    </Picker>

when trying to build i get "No property, bindable property, or event found for 'ItemsSource'" error.

Above that i have a label:

<Label Text ="{Binding SomeLabel, Source={x:Static local:MyModelHandler.MyModel}, Mode=OneWay}"></Label>

And that binding works perfectly

MyModelHandler is an static class that allowes only one Model

public static class MyModelHandler
{
    private static MyModel myModel = new MyModel();

    public static MyModel MyModel
    {
        get
        {
            return myModel;
        }
    }
}

And Model is simple:

public class MyModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private int selectedNumber = 1;
    private string someLabel = "";

    public IList<int> SomeList
    {
        get
        {
            return Enumerable.Range(1, 10).ToList();
        }
    }

    public int SomeListSelectedIndex
    {
        get
        {
            return SomeList.IndexOf(this.selectedNumberOfPeople);
        }
        set
        {
            this.selectedNumber = SomeList[value];
        }
    }

        public double SomeLabel
        {
            get
            {
                return this.someLabel;
            }
            set
            {
                this.someLabel= value;
            }
        }


    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
     }
...
}

Edit: Using Xamarin 4.3

2

2 Answers

1
votes

You might check your Xamarin.Forms version.

The ItemsSource property was introduced in Xamarin.Forms 2.3.4.184-pre1, see release notes here:

https://developer.xamarin.com/releases/xamarin-forms/xamarin-forms-2.3/2.3.4-stable/#2.3.4.184-pre1.

If you are using an older Xamarin.Forms version you will get the Xamarin.Forms XAML error "No property, bindable property, or event found for 'ItemsSource'".

1
votes

That is a very strange way to set up the binding context for a view. The fact you have to specify the source for each element adds a lot of extra boilerplate code.

Try setting the bindingcontext to the model in the the view constructor

BindingContext = new MyModel ();

Then the XAML becomes

<Picker Title="Number of People" 
        ItemsSource="{Binding SomeList}" 
        SelectedItem="{Binding SomeListSelectedIndex}">
</Picker>

Or use a proper MVVM framework and save yourself a lot of grief. I can recommend FreshMvvm.

https://github.com/rid00z/FreshMvvm