Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set focus on Entry inside Listview? Xamarin Forms

I have a listview and each row contains a label and an entry. How can I set focus for the entry if a row was tapped. My listview is generated dynamically.

 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 show up whenever user tap a particular row regardless of any position.

like image 376
jdk Avatar asked Jun 29 '17 06:06

jdk


1 Answers

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 code behind

    private void ViewCell_Tapped(object sender, EventArgs e)
    {
        ViewCell vs = (ViewCell)sender;
        var entry = vs.FindByName<Entry>("myEntry");
        entry?.Focus();
    }
like image 138
Yuri S Avatar answered Oct 28 '22 07:10

Yuri S