In ScrollView, first entry is becoming focus. i need to stop it.

Sowndarrajan Vijayaragavan 450 Reputation points
2024-05-30T18:40:37.5966667+00:00

User's image

User's image

    <ScrollView Grid.Column="1" x:Name="myScrollView" WidthRequest="500" >

        <Border StrokeThickness="2">

            <StackLayout>

            <Entry x:Name="MainEntry" Placeholder="MainEntry" />

            <Entry x:Name="Sub1Entry" Placeholder="Sub1Entry" />

                <Grid RowDefinitions="Auto,Auto,Auto" Padding="10">

                <Entry Grid.Row="0" x:Name="Sub2Entry"  Placeholder="Sub2Entry" />

                    <StackLayout  Grid.Row="1" x:Name="MainStackLayout" HeightRequest="700" BackgroundColor="red">

                        <Label Text="Tap here to show MainStackLayout" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />

                        <!-- Add more content if needed -->

                        <StackLayout.GestureRecognizers>

                            <TapGestureRecognizer Tapped="OnMainStackLayoutTapped" />

                        </StackLayout.GestureRecognizers>

                    </StackLayout>

                    <StackLayout Grid.Row="2" x:Name="ChildStackLayout" HeightRequest="250" BackgroundColor="Yellow" IsVisible="False">

                        <Label Text="This is the ChildStackLayout" />

                        <!-- Add more content if needed -->

                    </StackLayout>

                </Grid>

            </StackLayout>

        </Border>

    </ScrollView>


private void OnMainStackLayoutTapped(object sender, TappedEventArgs e)

{

    if (ChildStackLayout.IsVisible)

    {

        ChildStackLayout.IsVisible = false;

    }

    else

    {

        ChildStackLayout.IsVisible = true;

    }

}  

When the MainStackLayout is tapped ChildStackLayout.IsVisible will become true;
At that time it is scrolling to top and Focusing the MainEntry.
i need solution to unfocus it.

User's image

Windows 11

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-05-31T05:33:22.1866667+00:00

    Hello,

    At that time it is scrolling to top and Focusing the MainEntry.i need solution to unfocus it.

    You can do this by adding  myScrollView.Unfocus(); when you execute OnMainStackLayoutTapped like the following code.

    private async void OnMainStackLayoutTapped(object sender, TappedEventArgs e)
    
     {      
    
         if (ChildStackLayout.IsVisible)      { 
            ChildStackLayout.IsVisible = false;
         }
         else 
         { 
             ChildStackLayout.IsVisible = true; 
         } 
    await Task.Delay(100);
    myScrollView.Unfocus();
    await Task.Delay(100);        
    await ScrollView.ScrollToAsync(0, 170, false);
    
    }
    
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.