How to load the complex type property values in the Picker Editor in .NET MAUI DataForm (SfDataForm)?
The Syncfusion® .NET MAUI DataForm (SfDataForm) supports loading complex types of property values in the Picker Editor.
C#:
In .NET MAUI, you can define a model class with complex properties that can be loaded into a Picker editor within the DataForm.
public class ComplexModel
{
public string Name { get; set; }
public int DepartmentID { get; set; }
}
C#:
You can load a list of complex model objects into the Department property using a Picker editor. This is done by defining a method to provide the data source.
public object GetSource(string sourceName)
{
if(sourceName == "Department")
{
List<ComplexModel> departmentDetails = new List<ComplexModel>();
departmentDetails.Add(new ComplexModel() { Name = "Development", DepartmentID = 101 });
departmentDetails.Add(new ComplexModel() { Name = "HR", DepartmentID = 102 });
departmentDetails.Add(new ComplexModel() { Name = "Network", DepartmentID = 103 });
departmentDetails.Add(new ComplexModel() { Name = "Testing", DepartmentID = 104 });
return departmentDetails;
}
return new List<string>();
}
C#:
Use the GenerateDataFormItem event handler to customize the behavior of your DataForm.
protected override void OnAttachedTo(SfDataForm dataForm)
{
base.OnAttachedTo(dataForm);
if (dataForm != null )
{
dataForm.ItemsSourceProvider = new ItemSourceProvider();
dataForm.RegisterEditor("Department", DataFormEditorType.Picker);
dataForm.GenerateDataFormItem += OnGenerateDataFormItem;
}
}
C#:
Implement the OnGenerateDataFormItem event to specify the DisplayMemberPath and SelectedValuePath for the complex type property.
private void OnGenerateDataFormItem(object sender, GenerateDataFormItemEventArgs e)
{
if (e.DataFormItem!= null)
{
if (e.DataFormItem != null && e.DataFormItem.FieldName == "Department" && e.DataFormItem is DataFormComboBoxItem comboBoxItem)
{
comboBoxItem.DisplayMemberPath = "Name";
comboBoxItem.SelectedValuePath = "DepartmentID";
}
}
}
Output:
Conclusion
I hope you enjoyed learning how to load the complex type of editor values in the Picker editor in the .NET MAUI DataForm (SfDataForm).
Refer to our .NET MAUI DataForm feature tour page to learn about its other groundbreaking feature representations. You can also explore our .NET MAUI DataForm 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 DataForm and other .NET MAUI components.
Please let us know in the comment section if you have any queries or require clarification. Contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!