How to use Picker as Country Picker
This section explains how to use the SfPicker as Country Picker.
Refer to this Getting started documentation to create a simple SfPicker sample and configure it.
Step 1: Create a Country model class with the properties CountryName, CountryText and CountryCode.
Country Model.cs
public class Country { public string CountryImage { get; set; } public string CountryText { get; set; } public string CountryCode { get; set; } }
Step 2: Create a CustomView and in the Xaml bind the properties such as the CountryName, CountryText and CountryCode and we have set the CountryText based on the image name as like following code snippet.
CustomView.xaml
<Grid x:Name="customGrid" VerticalOptions="Start" HorizontalOptions="Start" Padding="90,20,0,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="40" /> <ColumnDefinition Width="auto" /> <ColumnDefinition Width="auto" /> </Grid.ColumnDefinitions> <Image Source="{Binding CountryImage}" HorizontalOptions="Center" VerticalOptions="Center" /> <Label Text="{Binding CountryText}" Padding="15,0,0,0" VerticalTextAlignment="Center" Grid.Column="1" /> <Label Text="{Binding CountryCode}" Padding="15,0,0,0" VerticalTextAlignment="Center" Grid.Column="2" /> </Grid>
CustomView.cs
Country _country; int i; public CustomView(Country country) { i = 0; _country = country; InitializeComponent(); foreach (char count in country.CountryImage) { i++; } _country.CountryText = country.CountryImage.Remove(i - 4); switch (_country.CountryText) { case "Germany": _country.CountryCode = "+49"; break; case "UAE": _country.CountryCode = "+971"; break; case "USA": _country.CountryCode = "+1"; break; case "UK": _country.CountryCode = "+44"; break; case "China": _country.CountryCode = "+86"; break; case "Canada": _country.CountryCode = "+1"; break; default: _country.CountryCode = "+91"; break; } this.BindingContext = _country; }
Step 3: Initialize the SfPicker in XAML page with the properties need to be assigned and added the items to the picker as like following code snippet.
<picker:SfPicker x:Name="picker" ItemHeight="45" HeaderText="Select Country" PickerMode="Dialog" PickerHeight="350" PickerWidth="350" ShowFooter="True"/> /// <summary> ///Collection to add the columnn items. /// </summary> ObservableCollection<object> countryNameCollection= new ObservableCollection<object>(); countryNameCollection.Add("India.png"); countryNameCollection.Add("UAE.png"); countryNameCollection.Add("USA.png"); countryNameCollection.Add("UK.png"); countryNameCollection.Add("Germany.png"); countryNameCollection.Add("Canada.png"); countryNameCollection.Add("China.png"); picker.ItemsSource = countryNameCollection;
Step 4: To display the picker as country picker by hooking the SfPicker.OnPickerItemLoaded event from picker and assign the custom view in PickerViewEventArgs.View property as like following code snippet
... /// <summary> /// Picker Item Loaded event. /// </summary> picker.OnPickerItemLoaded += PickerItemLoaded; ... /// <summary> ///Picker Item Loaded event. /// </summary> private void PickerItemLoaded(object sender, PickerViewEventArgs e) { Country country = new Country() { CountryName = e.Item.ToString() }; e.View = new CustomView(country); }
Output:
You can find the sample in the following link: Sample