简体   繁体   English

Xamarin Listview获取所选项目

[英]xamarin listview get selected item

Cannot figure out a proper way get an item from ListView. 无法找出从ListView获取项目的正确方法。

My XAML bindings: 我的XAML绑定:

            <ListView x:Name="MyListView" ItemTapped="MyListView_ItemTapped" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>

                            <Label Text="{Binding Name}"></Label>
                            <Label Text="{Binding Email}"></Label>
                            <Image Source="{Binding PhotoUrl}" WidthRequest="20" HeightRequest="20"></Image>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

All the data gets displayed and works fine. 所有数据都会显示并可以正常工作。 Class is called Forums: 该类称为论坛:

    [JsonProperty("results")]
    public List<Result> Results { get; set; }

    public class Result
    {
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("email")]
        public string Email { get; set; }
        [JsonProperty("photoUrl")]
        public string PhotoUrl { get; set; }
    }

I have made MyListView_ItemTapped Function and for now, I'm trying to display its name when the item is tapped on, but not sure what is a proper way to do it. 我已经制作了MyListView_ItemTapped函数,现在,我试图在点按该项目时显示其名称,但不确定执行此操作的正确方法是什么。 And I always think that I'm just bodging some random things together until I get something. 我一直认为我只是一些随机的东西结合在一起,直到得到一些东西。

        private void MyListView_ItemTapped(object sender, ItemTappedEventArgs e)
    {

        var index = forums.Results.IndexOf(e.Item as Forums.Result);
        DisplayAlert("Alert", forums.Results[index].Name, "OK");

    }

So if anyone could point me to the better direction or even give few better examples or just explain how should it be done. 因此,如果有人可以向我指出更好的方向,或者甚至给出一些更好的示例,或者只是解释应该如何做。

just cast e.Item to the correct type 只是将e.Item转换为正确的类型

var item = e.Item as Forums.Result;

// then use item.Name, etc...

You can cast to the correct Class 您可以投射到正确的班级

private void MyListView_ItemTapped(object sender, ItemTappedEventArgs e)
    {

        var index = forums.Results.IndexOf(e.Item as Forums.Result);
        var selectedItem = (Forums.Result)e.Item;
        if(selectedItem != null)
        {
           DisplayAlert("Alert", selected|Item.Name, "OK");
        }

    }

Oh and if you want to remove the selecteditem effect just 哦,如果您要删除selecteditem效果,

if (sender is ListView lv) lv.SelectedItem = null;

You can also use selected item property <ListView **SelectedItem="{Binding Result, Mode=TwoWay}">** 您还可以使用选定的项目属性<ListView **SelectedItem="{Binding Result, Mode=TwoWay}">**

code behind: 后面的代码:

`private Result _result; `私有结果_result;

public Result Result
{
    get { return _deviceSession; }
    set
    {
        SetProperty(ref _deviceSession, value);
    }
}`

From this class object you can able to get all data 从此类对象中,您可以获取所有数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM