这是我的ListView:
<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类的selected = 1或selected = 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
}
关于c# - 在Xamarin.Forms中获取ListView的选定项的ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38240142/