如何通过按钮聚焦ListView TextCell?

问题描述 投票:0回答:1

我需要通过点击按钮逐个关注TextCells。我试过listView.ScrollTo。

private void Button_Clicked_1(object sender, EventArgs e)
{
    listViewJson.ItemTapped += ListViewJson_ItemTapped;
}

private void ListViewJson_ItemTapped(object sender, ItemTappedEventArgs e)
{
    var focusing = e.Item;
    listViewJson.ScrollTo(focusing, ScrollToPosition.MakeVisible, true);
}
c# listview button xamarin xamarin.forms
1个回答
0
投票

首先,尝试定义索引以检测要选择的单元格。然后通过按钮点击更改索引,如:

int selectedIndex = 0;
private void MyBtn_Clicked(object sender, EventArgs e)
{
    if (selectedIndex == dataList.Count) selectedIndex = 0;
    myListView.SelectedItem = dataList[selectedIndex++];
}

当选择TextCell时,ListView的ItemSelected事件将触发,您可以将代码放入其中:

private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    //try to do something
}

这是我的代码,你指的是:

ObservableCollection<string> dataList = new ObservableCollection<string>();

int selectedIndex = 0;

public MainPage()
{
    InitializeComponent();

    for (int i=0; i<10; i++)
    {
        dataList.Add("item" + i);
    }

    myListView.ItemsSource = dataList;

    myListView.ItemSelected += MyListView_ItemSelected;

    MyBtn.Clicked += MyBtn_Clicked;
}

private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    //try to do something
}

private void MyBtn_Clicked(object sender, EventArgs e)
{
    if (selectedIndex == dataList.Count) selectedIndex = 0;
    myListView.SelectedItem = dataList[selectedIndex++];
}
© www.soinside.com 2019 - 2024. All rights reserved.