How do I set focus on an input inside a Listview? Xamarin forms

I have a listview and each line contains a label and an entry. How to set focus to write if line was clicked. My list is dynamically generated.

 void selected(object sender, SelectedItemChangedEventArgs e)
    {
        if (e.SelectedItem == null)
        {
            return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
        }
        TestReading item = (TestReading)e.SelectedItem;




        //comment out if you want to keep selections
        ListView lst = (ListView)sender;

        lst.SelectedItem = null;

    }

      

I want the soft keyboard to be displayed whenever the user presses a certain line regardless of any position.

+3


source to share


1 answer


Use Tapped

   <ListView x:Name="ItemsListView" SeparatorColor="LightGray" BackgroundColor="Green" RowHeight="60">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell Tapped="ViewCell_Tapped">
                    <StackLayout Padding="15, 5, 0, 0" Orientation="Horizontal" BackgroundColor="White">
                            <Entry x:Name="myEntry"  HorizontalOptions="FillAndExpand"/>
                            <Label Text = "{Binding ItemText}" FontSize="20" TextColor="Black" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

      



in the code behind

    private void ViewCell_Tapped(object sender, EventArgs e)
    {
        ViewCell vs = (ViewCell)sender;
        var entry = vs.FindByName<Entry>("myEntry");
        entry?.Focus();
    }

      

+3


source







All Articles