Here is my .xaml
<Picker x:Name="Title" SelectedItem="{Binding Title, Mode=TwoWay}" ItemsSource="{Binding Titles}" ItemDisplayBinding="{Binding Text}" Title="Title" />
<Entry x:Name="Name" Text="{Binding Name}" Placeholder="Name" />
Bound to a View Model .cs which looks like this
public class Person
{
public string Name { get; set; }
public string Title { get; set; }
List<SelectListItem> Titles = new(){
new SelectListItem { Text = "Mister", Value="Mr" }
new SelectListItem { Text = "Doctor", Value="Dr" }
...
}
}
Containing this data
Person person = new() { Name = "Bill Jones", Title = "Mr" };
So the picker displays the list just fine. But I have two issues.
- How do I get the picker to display the correct entry when it loads, in this case, default to
Mr
- If I change the value in the picker, how do I get the bound ViewModel to take on that entry? (Remember I want to store the selected value, not the displayed value). I know it works with a simple string list, but that's not what I want here.
It almost feels like I need an ItemValueBinding
property or something like that. (Obviously, I just made that up)
I've seen quite a lot of complicated code using INotifyPropertyChanged and doing clever bits of code in the SelectedIndexChanged
event. But if I have a lot of pickers on my page that seems like a lot of code I have to write.
Is there a simpler way that I might have missed, to achieve both requirements?