我有Grouped ListView,现在我需要访问该组中的项目,当我点击组列表中的项目时,我需要获得该项目的特定细节,
<ListView x:Name="DishesList" ItemsSource ="{Binding Details}" SeparatorVisibility="None" HasUnevenRows="true" ItemTapped = "OnItemSelected" IsGroupingEnabled="True" >
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding ="10" BackgroundColor="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackLayout Grid.Column= "1">
<Label Text ="{Binding Name}" TextColor ="#007aff" HorizontalTextAlignment="Center" VerticalTextAlignment="Center">
</Label>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding = "1" Spacing = "0" BackgroundColor="Transparent">
<Image x:Name="DishImage" Source = "{Binding DishImage}" HeightRequest="60" />
<Label x:Name="DishName" Text ="{Binding DishName}" TextColor ="#007aff" HorizontalTextAlignment="Center" VerticalTextAlignment="Center">
</Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
private async void OnItemSelected(object sender, SelectedItemChangedEventArgs eventArgs)
{
var selectedItem = (DishesModel)eventArgs.SelectedItem;
//How can I access the data of the Particular item?,Iam getting all the items which are present in that group but i need to get one among them on which clicked.
}
答案 0 :(得分:0)
如果您需要选择项目,请在XAML中使用 ItemSelected 代替 ItemTapped 。
你需要点击项目,然后将 SelectedItemChangedEventArgs 更改为 ItemTappedEventArgs ,将 eventArgs.SelectedItem 更改为 eventArgs.Item XAML.CS
答案 1 :(得分:0)
在ListView.ItemTemplate
的{{1}}内添加DataTemplate
,其中包含其包含的页面作为绑定源,并提供实现TapGestureRecognizer
的绑定路径{1}}点击组中的项目时要调用的内容,最后将ICommand
设置为绑定到自身(即组内的项目)。
类似的东西:
Command
CommandParameter
<ContentView.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference theContainingPage}, Path=BindingContext.TapCommand}" CommandParameter="{Binding .}"/>
</ContentView.GestureRecognizers>
该页面上的<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Forms_App_Project.Views.GroupListTapExample"
x:Name="theContainingPage">
<ContentPage.Content>
<ListView ItemsSource="{Binding ListOfItems}" IsGroupingEnabled="true">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Heading}" />
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView>
<Label Text="{Binding Name}" />
<ContentView.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference theContainingPage}, Path=BindingContext.TapCommand}" CommandParameter="{Binding .}"/>
</ContentView.GestureRecognizers>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
:public class Item
{
public string Name { get; set; }
}
public class GroupedItems : List<Item>
{
public string Heading { get; set; }
public List<Item> Items => this;
}