Xamarin forms set Picker SelectedItem

ajoy picture ajoy · Nov 25, 2017 · Viewed 8k times · Source

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;

Answer

sonicbabbler picture sonicbabbler · Mar 29, 2018

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;
            }
        }