获取 Xamarin.Forms 中列表视图的选定项的 ID
本文关键字:ID 视图 Xamarin Forms 列表 获取 | 更新日期: 2023-09-27 17:55:28
这是我的列表视图:
<ListView Grid.Column="0" Grid.Row="1" ItemTapped="itemTapped" x:Name="listofEmployee" BackgroundColor="{x:Static color:ColorResources.listBackgroundColor}" IsVisible="false">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#eee" Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Image x:Name="imgCheckUncheck" Source="btn_check_off.png" VerticalOptions="CenterAndExpand" HeightRequest="30" WidthRequest="30" >
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnImg_TapGestureRecognizerTapped" />
</Image.GestureRecognizers>
</Image>
<Label Text="{Binding empName}" VerticalOptions="CenterAndExpand" TextColor="{x:Static color:ColorResources.listTextColor}" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
此模型:
public class empList_Model
{
public string empName{ get; set;}
public int Selected{ get; set;}
public string id{ get; set;}
}
public static class empList_Data
{
public static List<empList_Model> getData ()
{
return new List<empList_Model> {
new empList_Model () {
empName = "Bryan Garret",Selected=0,id="1"
},
new empList_Model () {
empName = "James Simpson",Selected=0,id="2",
},
new empList_Model () {
empName = "Kathryn Newer",Selected=0,id="3"
},
new empList_Model () {
empName = "Amanda Stevens",Selected=0,id="4"
},
};
}
}
通过使用上面的代码,我想采取以下一些操作:
1)在图像点击时,设置类 empList_Model 的属性选择=1 或选定=0。
2)在图像点击时,在 DisplayAlert() 函数中显示属性"id"。
您需要在类的代码中实现"itemTapped"事件,因为不可能将命令绑定到视图模型。这应该看起来像这样:
public void OmItemTapped (object o, ItemTappedEventArgs e)
{
var dataItem = e.Item as empList_Model;
// now you can change the data item or trigger anything on the
// view model and provide the tapped instance as parameter
}