Articles in this section
Category / Section

How to display a property or member of a custom object in .NET MAUI Picker (SfPicker)?

2 mins read

In the Syncfusion® .NET MAUI Picker control, you can display a property or member of a custom object by following the below demonstration.

XAML:

Initialize the SfPicker Control.

<picker:SfPicker x:Name="picker">
  <picker:SfPicker.HeaderView >
      <picker:PickerHeaderView Height="40" Text ="Select a sport"  />
  </picker:SfPicker.HeaderView>
</picker:SfPicker>

C#:

Declare properties to display the items in a model class as shown below.

public class CountryInfo
{
  public string Country { get; set; }
  public string Sport { get; set; }
}

C#:

To bind the item source in the picker, set the item source collection to the ItemsSource property of the PickerColumn class.
Create an Observable collection with the modal class properties. Use the DisplayMemberPath property in the PickerColumn class to display a specific property instead of the entire object as the following code illustrates.

ObservableCollection<countryinfo> countryDetails = new ObservableCollection<countryinfo>
{
  new CountryInfo { Sport = "Baseball", Country = "USA" },
  new CountryInfo { Sport = "Pato", Country = "Argentina" },
  new CountryInfo { Sport = "Golf", Country = "Scotland" },
  new CountryInfo { Sport = "Oil wrestling", Country = "Turkey" },
  new CountryInfo { Sport = "Skiing", Country = "Norway" },
  new CountryInfo { Sport = "Cricket", Country = "England" },
  new CountryInfo { Sport = "Football", Country = "France" },
  new CountryInfo { Sport = "Badminton", Country = "Indonesia" },
};
PickerColumn pickerColumn = new PickerColumn()
{
  DisplayMemberPath = "Sport",
  ItemsSource = countryDetails,
  SelectedIndex = 3,
};
this.picker.Columns.Add(pickerColumn);

Output:

DisplayMemberPath.png

Download the complete sample on GitHub.

Conclusion:

I hope you enjoyed learning how to display a property or member of a custom object in .NET MAUI Picker.

Refer to our .NET MAUI Picker feature tour page to learn about its other groundbreaking feature representations. You can explore our .NET MAUI Picker documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Picker and other .NET MAUI components.

Please let us know in the following comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

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