获取Xamarin.Forms中ListView的所选项的ID

时间:2016-07-07 07:48:14

标签: c# listview xamarin.forms

这是我的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)在图像点按上,设置属性选择= 1或选择= 0类empList_Model。

2)在图像点按,显示属性&#34; id&#34;在DisplayAlert()函数中。

1 个答案:

答案 0 :(得分:3)

你需要实现&#34; itemTapped&#34;类中的代码,因为没有命令绑定到视图模型。这应该是这样的:

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
    }