Add to a DataForm ComboBox

Is there a simple way to add to the add items to a sfDataForm ComboBox?

Here is how I load a non-DataForm ComboBox:

  private void LoadComboBoxes()

  {
      visitcategory = new ObservableCollection<string>();
      visitcategory.Add("--Select One--");
      visitcategory.Add("Office Visit");
      visitcategory.Add("Established Patient");
      visitcategory.Add("New Patient");
      visitcategory.Add("Health and Behavioral Assessment");
      visitcategory.Add("Preventative Care Services");
      visitcategory.Add("Ophthalmological Services");


      VisitCategoryComboBox.ItemsSource = visitcategory;
      VisitCategoryComboBox.SelectedIndex = -1;
}



3 Replies 1 reply marked as answer

VM Vidyalakshmi Mani Syncfusion Team March 18, 2024 02:53 PM UTC

Hi Frederick,


Thank you for reaching out. To achieve your requirement, you can load the combo box using the `ItemSourceProvider`. Register the property as a `ComboBox` using the `RegisterEditor` method to make the editor function as a combo box editor, as shown in the code snippet below:


 

public string Country { get; set; }

 

dataForm.RegisterEditor("Country", DataFormEditorType.ComboBox);

dataForm.ItemsSourceProvider = new DataFormItemsSourceProvider();

 

 

public class DataFormItemsSourceProvider : IDataFormSourceProvider

{

        public object GetSource(string sourceName)

        {

            if (sourceName == "Country")

            {

                List<string> list = new List<string>()

                {

                    "USA",

                    "Japan",

                    "UK",

                };

 

                return list;

            }

 

            return new List<string>();

        }

}

 

 

 



We have prepared a simple sample demonstrating this. Please see the attached sample for your reference.


If you have any further questions or need assistance, please let us know!


Regards,

Vidyalakshmi M.




Attachment: GettingStarted_948d3ef8.zip

Marked as answer

FS Frederick Switzer replied to Vidyalakshmi Mani March 18, 2024 03:22 PM UTC

Thank you for that sample, but I'm looking for a solution that does not include the multiple Folders etc. 

My dataform has a number of properties that I want to make into comboboxes.  


  1. Can I make the changes in my .cs file (code behind)?
  2. How do I identify the fields if they are "dataform generated"  ?
    <dataform:SfDataForm

        x:Name="usersDataForm"
        AutoGenerateItems="True"
        GenerateDataFormItem="usersDataForm_GenerateDataFormItem"
        ValidateProperty="usersDataForm_ValidateProperty" />


Screenshot_2.jpg

I want to make the suffix field a combo box for "Jr., Sr.II ) and the Specialty field  (DR., Nurse,Therapist)




VM Vidyalakshmi Mani Syncfusion Team March 19, 2024 01:46 PM UTC

Hi Frederick,


Regarding your queries:


Handling Events in Code-Behind: Yes, you can manage the events and behavior of the DataForm directly in the xaml.cs file without the need for a separate behavior class.


Identifying "DataForm Generated" Fields: When the `AutoGenerateItems` property is set to `True`, editors are automatically generated based on the property type. To identify these generated editors, you can utilize the `GenerateDataFormItem` event, which is triggered for each editor creation. The `GenerateDataFormItemEventArgs` provides information about the currently generated editor.


Making Suffix and Specialty Fields ComboBoxes: To convert the `Suffix` and `Specialty` fields into ComboBoxes, you can register them as ComboBox editors using the `RegisterEditor` method. You can then populate these ComboBoxes by providing the itemsource in the `ItemSourceProvider` class as shown in the code snippet below.


 

this.dataForm.ItemsSourceProvider = new DataFormItemsSourceProvider();     

this.dataForm.RegisterEditor("Suffix", DataFormEditorType.ComboBox);

this.dataForm.RegisterEditor("Speciality", DataFormEditorType.ComboBox);

 

public class DataFormItemsSourceProvider : IDataFormSourceProvider

{

    public object GetSource(string sourceName)

    {

        if (sourceName == "Suffix")

        {

            List<string> list = new List<string>()

                {

                    "Jr.",

                    "Sr.",

                };

 

            return list;

        }

 

        if (sourceName == "Speciality")

        {

            List<string> list = new List<string>()

                {

                    "DR.",

                    "Nurse",

                    "Therapist",

                };

 

            return list;

        }

 

        return new List<string>();

    }

}

 

 



We have prepared a sample according to these suggestions. Please find the attached sample for your reference.


If you require further assistance, feel free to let us know.


Regards,

Vidyalakshmi M.


Attachment: GettingStarted_70a95952.zip

Loader.
Up arrow icon