Hello,
First, I am surprised that my Pickers BindingContext inherites from their parent StackLayout but not from the ContentPage. Is that normal?
Second, a picker inherites from its parent StackLayout BindingContext after the SelectedIndex is set. How to set the SelectedIndex after the Picker has inherited from its parent BindingContext?
this is my XAML code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AlmicantaratXF.Views.SightPage"
xmlns:i18n="clr-namespace:AlmicantaratXF.Views;assembly=AlmicantaratXF"
xmlns:viewModels="clr-namespace:AlmicantaratXF.ViewModels;assembly=AlmicantaratXF"
xmlns:converters="clr-namespace:AlmicantaratXF.Converters;assembly=AlmicantaratXF"
xmlns:behaviors="clr-namespace:AlmicantaratXF.Behaviors"
Title="{i18n:Translate titleSight}"
BackgroundColor="{StaticResource AppBackgroundColor}">
<!-- Title="{i18n:Translate titleSight}"> -->
<ContentPage.BindingContext>
<viewModels:SightPageViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout Orientation="Vertical"
Margin ="0"
Padding="0">
<!-- If the StackLayout.BindingContext is commented out, the Pickers binding does not work
as it is not commented out, the SightPAgeViewModel constructor is executed twice: one by ContentPage, second by StackLayout -->
<StackLayout.BindingContext>
<viewModels:SightPageViewModel/>
</StackLayout.BindingContext>
<Picker
x:Name="PickerBodyType"
Title="Body Type"
ItemsSource="{Binding BodyTypes, Mode=TwoWay}"
SelectedIndex="1"
WidthRequest="120"
Margin="0">
<!--<Picker.BindingContext>
<viewModels:SightPageViewModel/>
</Picker.BindingContext>-->
</Picker>
<Picker
x:Name="PickerLimb"
Title="limb"
WidthRequest="120"
Margin="0"
ItemsSource="{Binding Limb, Mode=TwoWay}"
SelectedIndex="0"
IsVisible="{Binding Source ={x:Reference PickerBodyType},
Path=SelectedIndex,
Converter= {StaticResource selectedItemToBool},
ConverterParameter=1}">
</Picker>
<Picker
Title="planets"
WidthRequest="120"
Margin="0"
ItemsSource="{Binding Planets, Mode=TwoWay}"
SelectedIndex="0"
IsVisible="{Binding Source ={x:Reference PickerBodyType},
Path=SelectedIndex,
Converter= {StaticResource selectedItemToBool},
ConverterParameter=2}" >
</Picker>
<Picker
Title="stars"
WidthRequest="120"
Margin="0"
ItemsSource="{Binding StarsCollection, Mode=TwoWay}"
SelectedIndex="0"
IsVisible="{Binding Source ={x:Reference PickerBodyType},
Path=SelectedIndex,
Converter= {StaticResource selectedItemToBool},
ConverterParameter=3}" >
</Picker>
</StackLayout>
</ContentPage.Content>
</ContentPage>