SfDataForm - focus specific item programatically (not at the start)

I have a data form with several fields, when I submit it I leave all fields untouched but the last, which I clear.

I didn't find a way to, after submit and clear of the last field, to set the focus on the last field programatically.


Te Focus() method on the dataForm seems to set the focus in the first empty field it finds, which in my case isn't always the desired one.


I also tryied to modify your suggestion for an initial focus in article below, trying to expose the proper entry to the dataForm in order to set the focus when I need to, but it doesn't work because apparently "DataFormTextEditor" isn't a public class so it can't be extended

https://support.syncfusion.com/kb/article/9798/how-to-programmatically-set-focus-to-editor-in-xamarin-forms-dataform-sfdataform


In general, the data form is great, but it would be useful to have more control over the proper entries/pickers and their events, because every behaviour I want to apply to the form, not being only filling fields, is hard to achieve, even the pretty common ones like dependant pickers


1 Reply

VM Vidyalakshmi Mani Syncfusion Team June 10, 2024 02:32 PM UTC

Hi Damian,


Thank you for contacting us. To achieve your requirement, you can use the `DataFormItemManager` class. In the `OnInitialize` method of this class, you can access the DataForm item and its associated view. By storing these in a dictionary, you can later access the specific DataForm item and set the focus by calling the `Focus` method on its view through a button click.


Here is a code example for your reference:


 

this._dataForm.ItemManager = new DataFormItemManagerEditorExt();

public class DataFormItemManagerEditorExt : DataFormItemManager

{

    // Dictionary to store DataForm items and their associated views   

   public Dictionary<DataFormItem, View> Views = new Dictionary<DataFormItem, View>();

 

    public override void InitializeDataEditor(DataFormItem dataFormItem, View editor)

    {

        if (editor is Entry entry)

        {

             // Add the DataForm item and its associated view to the dictionary

            Views.Add(dataFormItem, editor);

        }

    }

}

 


MainPage.xaml.cs

 

private void Button_Clicked(object sender, EventArgs e)

{

    foreach (KeyValuePair<DataFormItem, View> item in ((DataFormItemManagerEditorExt)dataForm.ItemManager).Views)

    {

        if (item.Key.FieldName == "GroupName")

        {

            // Set focus to the view if the field name matches

            item.Value.Focus();

        }

    }

}

 


We have also prepared a simple sample illustrating this implementation. Please refer to the attached sample for further guidance.


If you have any additional questions or need further assistance, please don't hesitate to ask.


Regards,

Vidyalakshmi M.



Attachment: GettingStarted_90709aa7.zip

Loader.
Up arrow icon