[英]How do you get the index number of the item being selected?
I want to DisplayAlert the index number of selected item in the ListView, my code currently just display the item_code but I want to display the index number in the ListView, thanks a lot. 我想在ListView中显示警报选定项目的索引号,我的代码当前仅显示item_code,但我想在ListView中显示索引号,非常感谢。
My .cs 我的.cs
lstView.ItemsSource = _sqLiteConnection.Table<ItemModel>();
lstView.ItemSelected += async (sender, e) =>
{
await DisplayAlert("Selected ",((ItemModel)e.SelectedItem).item_code
+ " was selected.", "OK");
};
My xaml.cs 我的xaml.cs
<ListView x:Name="lstView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout Orientation="Vertical" >
<Label Text="{Binding item_desc}" />
<Label Text="{Binding selling_price}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
View Model 查看模型
public class ItemModel
{
public string cost_method { get; set; }
public string item_code { get; set; }
public string item_desc { get; set; }
public string item_group_desc { get; set; }
public long item_id { get; set; }
public string stock_item { get; set; }
public string trnx_unit { get; set; }
public decimal selling_price { get; set; }
}
This question has already been answered in a few threads, one of which can be found here: 这个问题已经在几个线程中得到解答,其中一个可以在这里找到:
Xamarin Forms - Get position of item selected in Listview Xamarin表单-获取在Listview中选择的项目的位置
Short snippet of answer (credit to the above post): 简短的答案片段(对以上帖子的感谢):
var index = (myListView.ItemsSource as List<MyObject>).IndexOf (e.SelectedItem as MyObject);
public Class Students
{
public int sid { get; set; }
public string Name { get; set; }
}
listview.ItemSelected += Lst_ItemSelected;
private void Lst_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var index = (listview.ItemsSource as List<Students>).IndexOf(e.SelectedItem as Students); // to get a index value or postion value
((ListView)sender).SelectedItem = null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.