如何通过按钮聚焦ListView的TextCell?

时间:2017-12-26 08:58:03

标签: c# listview button xamarin xamarin.forms

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

I     SUM_RESULT
-----------------
2      34890
3      35546
4      35578
5      35620

1 个答案:

答案 0 :(得分: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++];
}