简体   繁体   English

xamarin.forms ListView ItemSelected

[英]xamarin.forms ListView ItemSelected

I have a question regarding the ItemSelected() event on a ListView element.我对 ListView 元素上的 ItemSelected() 事件有疑问。

My ListView is based on a DataTemplate like this:我的 ListView 基于这样的 DataTemplate:

<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.5*"/>
                    <ColumnDefinition Width="0.5*"/>
                </Grid.ColumnDefinitions>
                <Label Text="{Binding Name}" FontAttributes="Bold" />
                <Button Text="More" Clicked="MoreInfo" c="{Binding Name}"/>
            </Grid>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

Which gets an Array of PlaceItems which is structured below.它得到一个 PlaceItems 数组,其结构如下。

private PlaceItem[] places = {
    new PlaceItem("Theo's huis"),
    new PlaceItem("Kerk op de berg"),
    new PlaceItem("Hostel Stay Okay")
};

public class PlaceItem
    {
        public PlaceItem(string Name, double Lat = 0.0, double Lng = 0.0)
        {
            this.Name = Name;
            this.Lat = Lat;
            this.Lng = Lng;
        }

        public string Name { get; set; }
        public string Location { get; set; }
        public double Lat { get; set; }
        public double Lng { get; set; }
    }

This is my SelectedItem() method:这是我的 SelectedItem() 方法:

placesListView.ItemSelected += (object sender, SelectedItemChangedEventArgs e) =>
{
    DisplayAlert("ItemSelected", e.SelectedItem.ToString(), "Ok");
};

When I select an item it always alerts with the string "KK2.PlaceItem" where KK2 is my namespace.当我选择一个项目时,它总是用字符串“KK2.PlaceItem”发出警报,其中 KK2 是我的命名空间。 So how do I send data to the ItemSelected event from the ListView Item?那么如何从 ListView 项向 ItemSelected 事件发送数据呢? Like sending the Item index in the Array, or sending the Lat or Lng properties from the object.就像在 Array 中发送 Item 索引,或者从对象发送 Lat 或 Lng 属性一样。

I hope that I gave you enough information to help me with this problem.我希望我给了你足够的信息来帮助我解决这个问题。 Xamarin is new for me but I'm willing to learn it. Xamarin 对我来说是新的,但我愿意学习它。

Thanks in advance.提前致谢。

Theo西奥

placesListView.ItemSelected += (object sender, SelectedItemChangedEventArgs e) =>
{
    var item = (PlaceItem) e.SelectedItem;

    // now you can reference item.Name, item.Location, etc

    DisplayAlert("ItemSelected", item.Name, "Ok");
};

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

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