Issue
Xamarin Form Picker - ItemDisplayBinding not working
I am trying to populate my Picker with a property of the objects in the ItemsSource, essentially exactly how it is documented here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/picker/populating-itemssource#populating-a-picker-with-data-using-data-binding
Here is what I have so far:
Xaml
<ContentPage.BindingContext>
<local:Model />
</ContentPage.BindingContext>
<Picker Title="Select a Profession" ItemsSource="{Binding Professions}" ItemDisplayBinding="{Binding Name}" />
Model - Binded
public List<Profession> Professions { get; set; }
Profession Object
public class Profession
{
public string Name { get; set; }
}
So going by Microsoft's docs, the Name property should display, but it does not and in Visual Studio (For Mac - 2019), it doesn't event recognise Name as a property, just gives me the option of Professions again.
Am I doing anything obviously wrong?
Solution
I fixed this by changing List
to an ObservableCollection
. I was trying to populate my picker in an async method and an ObservableCollection
implements the INotifyCollectionChangedInterface
Reference: ObservableCollection<> vs. List<>
Answered By - Liam Kenny
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.