I have a picker on my Xamarin form, bound to a list, and I would like that it has the first item of that list already selected when the form is opening.
My list looks like this
Languages = new List<string>();
Languages.Add("English");
Languages.Add("Nederlands");
Languages.Add("русский");
I tried like this
<Picker x:Name="PickerLanguage" Title=""
TitleColor="#004973"
FontSize="24"
ItemsSource="{Binding Languages}"
SelectedIndex="0"
SelectedItem="English">
</Picker>
and like this
<Picker x:Name="PickerLanguage" Title=""
TitleColor="#004973"
FontSize="24"
ItemsSource="{Binding Languages}"
SelectedIndex="0"
SelectedItem="0">
</Picker>
Both attemps above are based on the answer of this question
But is does not works.
How can I have it so that when the form opens, English
is set as selected item/index already ?
CodePudding user response:
create a property for your selected item in your c# then bind it to your picker :
private string selectedObj;
public string SelectedObj
{
get{ return selectedObj;}
set{ selectedObj= value ;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedObj"));}
}
then you can set your value to the selected object any place in your code:
SelectedObj = "English";
then bind it to picker in xaml:
<Picker x:Name="PickerLanguage" Title=""
TitleColor="#004973"
FontSize="24"
ItemsSource="{Binding Languages}"
SelectedItem="{Binding SelectedObj}">
</Picker>