Specify Combo Box Items

  • 3 minutes to read

DataFormView generates ComboBoxEdit editors for the underlying data object’s properties of the enum type. You can also bind a combo box editor to a collection that stores objects of any type - for example, primitive types (strings, integers, etc.) or custom objects.

To specify an item source for the data form’s combo box editor, follow the steps below:

  1. Create a class that implements the IPickerSourceProvider interface. An instance of this class (ComboBoxDataProvider, in this example) serves as the data source for all combo box editors within the data form.
  2. Override the GetSource method so that it returns an item source for a combo box based on the data object property to which the editor is bound.
  3. Set the DataFormView.PickerSourceProvider property to a ComboBoxDataProvider class instance.
  4. Use the DataFormComboBoxEditorAttribute attribute to assign a combo box editor to the data object’s property.
    In this example, the data form is bound to the EmployeeInfo object and uses combo box editors for the following properties of this object:
    • Department
      The data provider’s GetSource method returns a list of DepartmentInfo objects that serve as the data source for the drop-down item list of the Department combo box. Use the ValueMember and DisplayMember parameters to specify data source field names that supply item values (integer values that define department codes) and display strings (department names), respectively.
    • Status
      The data provider’s GetSource method returns a list of strings that are displayed as items within the Status combo box and used to set the data object’s Status property value.

View Example

using Xamarin.Forms;
using System.Collections;
using System.Collections.Generic;
using DevExpress.XamarinForms.DataForm;

namespace DataForm_ComboBoxEditor {
    public class DepartmentInfo {
        public int DepartmentCode { get; set; }
        public string DepartmentName { get; set; }

        public DepartmentInfo(int code, string name) {
            DepartmentCode = code;
            DepartmentName = name;
        }
    }

    public class EmployeeInfo {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        [DataFormComboBoxEditor(ValueMember = "DepartmentCode", DisplayMember = "DepartmentName")]
        public int Department { get; set; }

        [DataFormComboBoxEditor]
        public string Status { get; set; }
    }

    public class ComboBoxDataProvider : IPickerSourceProvider {
        public IEnumerable GetSource(string propertyName) {
            if (propertyName == "Department") {
                return new List<DepartmentInfo>() {
                    new DepartmentInfo(0, "Sales"),
                    new DepartmentInfo(1, "Support"),
                    new DepartmentInfo(2, "Shipping"),
                    new DepartmentInfo(3, "Engineering"),
                    new DepartmentInfo(4, "Human Resources"),
                    new DepartmentInfo(5, "Management"),
                    new DepartmentInfo(6, "IT")
                };
            }
            if (propertyName == "Status") {
                return new List<string>() {
                    "Salaried",
                    "Commission",
                    "Terminated",
                    "On Leave"
                };
            }
            return null;
        }
    }

    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
            dataForm.DataObject = new EmployeeInfo();
            dataForm.PickerSourceProvider = new ComboBoxDataProvider();
        }
    }
}