如何在Listview中设置焦点? Xamarin表格

时间:2017-06-29 06:19:17

标签: c# listview xamarin.forms

我有一个列表视图,每行包含一个标签和一个条目。如果点击了一行,如何为条目设置焦点。我的列表视图是动态生成的。

 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;

    }

我希望只要用户点击特定行而不管任何位置,都会显示软键盘。

1 个答案:

答案 0 :(得分:4)

使用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>

中的代码

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