How to autofocus Entry control within SfPopup

Hello,

I have a SfPopupLayout inside my Content Page. It contains a SfTextLayoutInput with an Entry, see code example below. 

I would like to automatically focus the Entry control once the SfPopup has been opened. I would do this in the Opened event handler, but how can I access the Entry control to call its .Focus() method? Is there a way how to achieve that?

thanks

Jiri


 <popupLayout:SfPopupLayout >
                <popupLayout:SfPopupLayout.PopupView >
                    <popupLayout:PopupView AppearanceMode="TwoButton"
                                           AcceptCommand="{Binding EditListNameDialogAcceptCommand}"
                                           DeclineCommand="{Binding EditListNameDialogDeclineCommand}" 
                                           HeaderTitle="Something"
                                           AcceptButtonText="OK"
                                           DeclineButtonText="Cancel"
                                           HeightRequest="205">
                        <popupLayout:PopupView.ContentTemplate>
                            <DataTemplate>
                                <StackLayout Padding="10,5,10,5" Orientation="Vertical">
                                    <textInputLayout:SfTextInputLayout  HelperText="{Binding ListNameDialogHelperText}">
                                        <Entry Text="{Binding ListName, Mode=TwoWay}" x:Name="txtListName" />
                                    </textInputLayout:SfTextInputLayout>
                                </StackLayout>
                            </DataTemplate>
                        </popupLayout:PopupView.ContentTemplate>
                    </popupLayout:PopupView>
                </popupLayout:SfPopupLayout.PopupView>
            </popupLayout:SfPopupLayout>

3 Replies

KK Karthikraja Kalaimani Syncfusion Team May 25, 2020 06:34 AM UTC

Hi Jiri,

Please refer to the below code snippet to achieve your requirement on Opened event of the SfPopupLayout.

Code snippet :

 
 
        private void PopupLayout_Opened(object sender, EventArgs e) 
        { 
            var nativeObject = (object)popupLayout.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name.Equals("NativeObject")).GetValue(popupLayout); 
            var formsPopupviewContentTemplate = nativeObject.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("formsPopupViewContentTemplate")).GetValue(nativeObject); 
            var gridContent = (Grid)formsPopupviewContentTemplate.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("contentGrid")).GetValue(formsPopupviewContentTemplate); 
            (gridContent.Children[4] as Entry).Focus(); 
        } 

Regards,
Karthik Raja 



JM Jiri Matejka May 25, 2020 08:57 PM UTC

Thanks Karthik, 

I had to slightly modify the code like that:

var nativeObject = (object)listNameDialog.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name.Equals("NativeObject")).GetValue(listNameDialog);
            var formsPopupviewContentTemplate = nativeObject.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("formsPopupViewContentTemplate")).GetValue(nativeObject);
            var txtCtrl = (formsPopupviewContentTemplate as StackLayout).FindByName<Entry>("txtShoppingListName");
            txtCtrl?.Focus();

the "listNameDialog" refers to the popupView. 

and it works perfectly. 

Thanks for your help!

Jiri





KK Karthikraja Kalaimani Syncfusion Team May 26, 2020 06:11 AM UTC

Hi Jiri,

Thank you for the update. We glad to know that your requirement has been achieved. Please let us know if you have any further queries on this. We are happy to help you.

Regards,
Karthik Raja 


Loader.
Up arrow icon