我有一个关于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项目向ItemSelected事件发送数据?比如向数组中发送项目索引,或者从对象发送Lat或Lng属性。
我希望我给你足够的信息来帮助我解决这个问题。对我来说,Xamarin是新的,但我愿意学习它。
提前致谢。
Theo