Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value from my picker? Xamarin forms

I have trouble to get out the selected string from my picker. 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 only get the value "info1" even if i select number 2. It has something to do with the SelectedIndex so it only picks the "1", but I am not sure how to write the code to make it work for the selected item.

like image 607
DiddanDo Avatar asked Dec 28 '15 23:12

DiddanDo


People also ask

How to get selected date from datepicker in Xamarin forms?

The Xamarin. Forms SfPicker allows to get current selected date using the SelectedItem property. In the selection changed event, use the selected item property to get the current selected Xamarin.

How do you bind a list to picker in xamarin?

When binding to a list of objects, the Picker must be told which property to display from each object. This is achieved by setting the ItemDisplayBinding property to the required property from each object. In the code examples above, the Picker is set to display each Monkey.Name property value.


1 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 new Xamarin Forms Release 2.3.4 exists the

Bindable Picker

you can set an ItemSource

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

and Bind the SelectedIndex property

SelectedIndex="{Binding CountriesSelectedIndex}"
like image 177
Alessandro Caliaro Avatar answered Oct 19 '22 14:10

Alessandro Caliaro