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?
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/
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly