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 set a blank value to a picker editor in Xamarin.Forms DataForm?

Platform: Xamarin.Forms |
Control: SfDataForm |
Published Date: April 9, 2019 |
Last Revised Date: April 10, 2019

DataForm allows you customize the ItemsSource of picker editor. This article explains how to add a blank value to a picker editor in Xamarin.Forms DataForm.

 

Here, the Country data field is loaded as picker editor.

 

public class ContactInfo
{
     public string Name { get; set; }
     public string Address { get; set; }
     public string Country { get; set; }
     public string Email { get; set; }
} 

 

Using SourceProvider, you can set ItemsSource to picker editor with blank entry added to that list.

 

Here, the CountryName is set to ItemsSource to Picker.

 

public class SourceProviderExt : SourceProvider
{
    public override IList GetSource(string sourceName)
    {
        var CountryName = new List<string>();
        if (sourceName == "Country")
        {
            if(Device.RuntimePlatform == Device.Android)
                CountryName.Add(" ");
            else
                CountryName.Add("");
            CountryName.Add("Argentina");
            CountryName.Add("Brazil");
            CountryName.Add("Sweden");
        }
        return CountryName;
    }
}

 

 

Note:

In Xamarin.Forms.Android, “picker shows 0 for empty string set in picker ItemsSource”. This behavior is due to NumberPicker will be loaded for DataFormPickerEditor in Xamarin.Forms.Android renderer, and then the empty string value will be converted to 0. To resolve this, add a white space to blank entry item in Xamarin.Forms.Android picker.

Refer to this discussion to know about the issue regarding NumberPicker.

 

Behavior allows you add functionality to DataForm. Xamarin.Forms behaviors are created by deriving from the Behavior or Behavior<T> class.

 

public class DataFormBehavior : Behavior<ContentPage>
{
        SfDataForm dataForm = null;
        protected override void OnAttachedTo(ContentPage bindable)
        {
            base.OnAttachedTo(bindable);
            dataForm = bindable.FindByName<SfDataForm>("dataForm");
            dataForm.DataObject = new ContactInfo();
            dataForm.RegisterEditor("Country", "Picker");
            dataForm.SourceProvider = new SourceProviderExt();
        }
 }

 

Example: DataFormPickerEditor

 

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