How to get value from my collector? Xamarin Forms

It’s hard for me to get out of a select stringfrom my collector. This is my code:

XAML:

<Picker x:Name="thePicker" >
    <Picker.Items>
        <x:String>info1</x:String>
        <x:String>info2 </x:String>
    </Picker.Items>
</Picker>

CODE:

thePicker.SelectedIndex = 1; //here is the problem i suppose, any idea what i should type?
var ourPickedItem = thePicker.Items[thePicker.SelectedIndex];

Now I get the value "info1", even if I select number 2. It has something to do with it SelectedIndex, so it only selects "1", but I'm not sure how to write the code to make it work for the selected item.

+4
source share
2 answers

You should take a look at this :

picker.SelectedIndexChanged += (sender, args) =>
{
    if (picker.SelectedIndex == -1)
    {
        boxView.Color = Color.Default;
    }
    else
    {
        string colorName = picker.Items[picker.SelectedIndex];
        boxView.Color = nameToColor[colorName];
    }
};

Otherwise, in the new release of version 2.3.4, Xamarin Forms exists

Bindable picker

you can set ItemSource

<Picker ItemsSource="{Binding Countries}" />

and bind the SelectedIndex property

SelectedIndex="{Binding CountriesSelectedIndex}"
+10

XLabs . :

<ContentPage x:Class="XLabs.Samples.Pages.Controls.ExtendedPickerPage"
         xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms"
         Title="Picker">
<ContentPage.Content>
    <StackLayout x:Name="myStackLayout">
        <Label Text="Xaml:" />
        <controls:ExtendedPicker x:Name="myPicker"
                                 DisplayProperty="FirstName"
                                 ItemsSource="{Binding MyDataList}"
                                 SelectedItem="{Binding TheChosenOne}" />
    </StackLayout>
</ContentPage.Content>
0

Source: https://habr.com/ru/post/1622029/


All Articles