Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin forms set Picker SelectedItem

I am working with a xamarin Forms. I am using Picker for DropDownList.

How can I set selectedItem to Picker?

My code

<Picker x:Name="VendorName" Title="Select" ItemDisplayBinding="{Binding VendorName}" SelectedItem="{Binding VendorName}" Style="{StaticResource PickerStyle}"></Picker>

and server side code is

Device.BeginInvokeOnMainThread(() =>
{
VendorName.ItemsSource = VendorList;
});

var currentVendor = new List<Vendor>();
currentVendor.Add(new Vendor { VendorID = "111", VendorName = "aaaa" });

VendorName.SelectedItem = currentVendor;
like image 709
ajoy Avatar asked Nov 25 '17 09:11

ajoy


2 Answers

This may not be the most efficient but you could loop to find the index and set that way.

for (int x = 0; x <  VendorList.Count; x++)
        {
            if (VendorList[x].VendorName == currentVendor .VendorName )
            {
                VendorName.SelectedIndex = x;
            }
        }
like image 194
sonicbabbler Avatar answered Oct 02 '22 10:10

sonicbabbler


After adding all values as list in Picker

just treat with it as an array

so if you want to set selected item just set selected item index

currentVendor.SelectedIndex = 0;

zero means you make selected item is the first one you added to Picker

like image 23
Mahmoud Kamel Avatar answered Oct 02 '22 08:10

Mahmoud Kamel