Articles in this section
Category / Section

How to set a blank value to a picker editor in Xamarin.Forms DataForm?

1 min read

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

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied