Git Product home page Git Product logo

dropdown-collection-update-dataform's Introduction

How to add entered text to the dropdown collection in Xamarin.Forms DataForm (SfDataForm)

You can add the entered text to the DropDown editor’s collection in Xamarin.Forms SfDataForm by using a custom editor.

You can create and add the custom editor to SfDataForm by overriding the DataFormEditor class, where the CustomDropDownEditor is inherited using DataFormDropDownEditor.

Refer to the online user guide documentation for creating a new custom editor in DataForm.

You can also refer the following article.

https://www.syncfusion.com/kb/11402/how-to-add-entered-text-to-the-dropdown-collection-in-xamarin-forms-dataform-sfdataform

C#

DropDown editor modified by customizing DataFormDropDownEditor. When unfocused from dropdown entered text added to the collection.

public class CustomDropDownEditor : DataFormDropDownEditor
{
    DataFormDropDownItem dropDownItem;
    SfDataForm sfDataForm;
    public CustomDropDownEditor(SfDataForm dataForm) : base(dataForm)
    {
        this.sfDataForm = dataForm;
    }
    protected override void OnInitializeView(DataFormItem dataFormItem, SfComboBox view)
    {
        base.OnInitializeView(dataFormItem, view);
        view.IsEditableMode = true;
        view.Unfocused += View_Unfocused; 
        dropDownItem = dataFormItem as DataFormDropDownItem;
    }
    private void View_Unfocused(object sender, Xamarin.Forms.FocusEventArgs e)
    {
        var dropDown = sender as SfComboBox;
        var text = dropDown.Text;
    if (string.IsNullOrEmpty(text))
        return;
        var selectedItem = dropDown.SelectedItem;
        if (selectedItem == null || (selectedItem as Details)?.Name?.ToString() != text)
        {
            dropDownItem.ItemsSource.Add(new Details { Name = text });
            (DataForm.DataObject as Address).Country = text;
            sfDataForm.UpdateEditor("Country");
        }
    }
  protected override void OnUnWireEvents(SfComboBox view)
  {
     base.OnUnWireEvents(view);
     view.Unfocused -= View_Unfocused;
  }
}

Refer to the following code example for binding DataObject and register the editor using RegisterEditor as CustomDropDownEditor to make data form items as a custom editor in DataForm.

C#

Customized DropDown editors registered to DataForm.

public class DataFormBehavior : Behavior<ContentPage>
{
    SfDataForm dataForm;
    protected override void OnAttachedTo(ContentPage bindable)
    {
        base.OnAttachedTo(bindable);
        dataForm = bindable.FindByName<SfDataForm>("dataForm");
        dataForm.SourceProvider = new SourceProviderContactForm();
        dataForm.RegisterEditor("DropDown", new CustomDropDownEditor(dataForm));
        dataForm.RegisterEditor("Country", "DropDown");
        dataForm.AutoGeneratingDataFormItem += DataForm_AutoGeneratingDataFormItem;
    }
    private void DataForm_AutoGeneratingDataFormItem(object sender, AutoGeneratingDataFormItemEventArgs e)
    {
        if (e.DataFormItem != null)
        {
            if (e.DataFormItem.Name == "Country")
            {
                (e.DataFormItem as DataFormDropDownItem).DisplayMemberPath = "Name";
                (e.DataFormItem as DataFormDropDownItem).SelectedValuePath = "Name";
            }
        }
    }
    protected override void OnDetachingFrom(ContentPage bindable)
    {
        base.OnDetachingFrom(bindable);
        dataForm.AutoGeneratingDataFormItem -= DataForm_AutoGeneratingDataFormItem;
    }
}

Output

DropDownCollectionUpdate

dropdown-collection-update-dataform's People

Contributors

jayaleshwari avatar sarubala20 avatar vinothkumar-ganesan avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.