Articles in this section

How to load custom objects in Xamarin.Forms SfPicker

Using the DisplayMemberPath property in Xamarin.Forms SfPicker, you can load custom objects. To achieve this, create a class, and set property of that class as a collection of objects into the DisplayMemberPath property.

 

The following steps explain how to load custom objects using the DisplayMemberPath property in Xamarin.Forms SfPicker.

 

Step 1: Create an app using Xamarin.Forms SfPicker control with all the required assemblies.

 

Step 2: Create a ViewModel class and add the items that need to be displayed in Xamarin.Forms SfPicker. The following code sample demonstrates this.

 

public class Category
{
    public string Name { get; set; }
    int ID { get; set; }
    public List<Category> SubCategories { get; set; }
}

 

Step 3: Set the “Name” property as a collection of objects into DisplayMemberPath of Xamarin.Forms SfPicker.

 

public MainPage()
{
    InitializeComponent();
    CategoryViewModel categoryViewModel = new CategoryViewModel();
    ObservableCollection<object> collection = new ObservableCollection<object>();
    collection.Add(categoryViewModel.Countries);
    List<Category> subCollection = new List<Category>();
    foreach (var item in categoryViewModel.Countries)
    {
        subCollection.AddRange(item.SubCategories);
    }
    collection.Add(subCollection);
    picker.ItemsSource = collection;
    picker.DisplayMemberPath = new ObservableCollection<object> { "Name", "Name" };
    this.BindingContext = categoryViewModel;
}

 

Output

 

Loading Custom Object

 

You can find the sample from this link: Sample

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