How to show dropdown list in Xamarin Forms?


I am new to C# Xamarin.Forms and i would like to know how can i create dropdown list in Xamarin.Forms like we do in HTML using

<select id="TestDDL">
   <option>Test</option>
</select>

So how to achieve it?


Asked by:- pika
0
: 16214 At:- 2/22/2018 1:14:11 PM
C# Xamarin Forms Dropdown list in Xamarin







1 Answers
profileImage Answered by:- manish

Try using Picker in Xamarin.Form, which is similar to HTML dropdown list, here is example code snippet using Xaml

<Picker x:Name="picker" Title="Select a color">
  <Picker.ItemsSource>
    <x:Array Type="{x:Type x:String}">
      <x:String>Blue</x:String>
      <x:String>Green</x:String>
     
    </x:Array>
  </Picker.ItemsSource>
</Picker>

in C#

var colorList= new List<string>();
colorList.Add("Red");
colorList.Add("Yellow");
colorList.Add("Blue");

var picker = new Picker { Title = "Select a color" };
picker.ItemsSource = colorList;

You can also call C# code, when value of picker changes using  SelectedIndexChanged event by binding to funtion, it will fire the method like below:

void OnPickerSelectedIndexChanged(object sender, EventArgs e)
{
  var picker = (Picker)sender;
  int selectedIndex = picker.SelectedIndex;

  if (selectedIndex != -1)
  {
    //do somthing here
    //(string)picker.ItemsSource[selectedIndex];
  }
}

For reference you use https://developer.xamarin.com/guides/xamarin-forms/user-interface/picker/populating-itemssource/

2
At:- 2/22/2018 3:44:40 PM
thanks that's what i was looking for :) 0
By : pika - at :- 2/23/2018 7:06:54 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use