We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to use Picker as Country Picker

Platform: Xamarin.Forms |
Control: SfPicker |
Published Date: April 7, 2020 |
Last Revised Date: April 7, 2020

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:

Country Picker Image

 

You can find the sample in the following link: Sample

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile