listview - xamarin.forms ListView ItemSelected

标签 listview xamarin xamarin.forms

我有一个关于 ListView 元素上的 ItemSelected() 事件的问题。

我的 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>

它获取一个 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; }
    }

这是我的 SelectedItem() 方法:

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

当我选择一个项目时,它总是用字符串“KK2.PlaceItem”发出警报,其中 KK2 是我的命名空间。 那么如何从 ListView Item 向 ItemSelected 事件发送数据呢? 就像发送数组中的项目索引,或者从对象发送 Lat 或 Lng 属性一样。

我希望我向您提供了足够的信息来帮助我解决这个问题。 Xamarin 对我来说是新的,但我愿意学习它。

提前致谢。

西奥

最佳答案

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");
};

关于listview - xamarin.forms ListView ItemSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39707103/

相关文章:

xamarin.forms - 如何获取导航栏背景颜色?

Android:在具有复杂行的ListView上进行fling检测

c# - 如何使用 Xamarin iOS 在 MugenMvvm 中使用自定义控件绑定(bind)到事件

java - 进度对话框未显示在 AsyncTask 中

android - Xamarin Camera2Basic 示例在 UnlockFocus 调用后引发异常

xaml - Xamarin TabbedPages MVVM 绑定(bind)

ios - 使用 Mac 代理在 Visual Studio 上调试 Xamarin.Forms iOS 应用

c# - 获取在 ExportRenderer 中调用 PageRenderer 的 ContentPage 的类型

android - 从原始文件夹播放声音,与 ListView 的每一行成比例

java - 为什么ListView显示对象地址而不是内容?