Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting Index of Tapped collectionview item xamarin

I have a CollectionView that is bound to Agent data (from the Valorant-api)

private async void ShowAgents()
    {
        Root agent = await AgentRepository.GetAgent();
        Agents = new ObservableCollection<Data>(agent.data);

        lvwAgents.ItemsSource = Agents;
    }

The xaml:

<CollectionView
            x:Name="lvwAgents"
            Grid.Row="5"
            Grid.ColumnSpan="2"
            HorizontalScrollBarVisibility="Never"
            Margin="0,-25,-75,0">
            
            <CollectionView.ItemsLayout>
                <LinearItemsLayout ItemSpacing="10" Orientation="Horizontal" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate >
                <DataTemplate>
                    <Grid>
                        <pcview:PancakeView 
                        CornerRadius="60,60,60,60"
                        HeightRequest="200" 
                        BackgroundGradientStartPoint="0,0" 
                        BackgroundGradientEndPoint="1,0"
                        VerticalOptions="End">
                            <pcview:PancakeView.BackgroundGradientStops>
                                <pcview:GradientStopCollection>
                                    <pcview:GradientStop Color="{Binding backgroundGradientColors[0]}" Offset="0" />
                                    <pcview:GradientStop Color="{Binding backgroundGradientColors[1]}" Offset="0.75" />
                                </pcview:GradientStopCollection>
                            </pcview:PancakeView.BackgroundGradientStops>
                            <StackLayout>
                                <Label Text="{Binding displayName}" Padding="30,75,0,0" FontFamily="GilroyExtraBold" TextColor="White" VerticalOptions="Center" FontSize="30"/>
                                <Label Text="Click to check agent info" Padding="30,0,40,0" FontFamily="FontCamptonMedium" TextColor="White" VerticalOptions="Center" FontSize="20"/>

                            </StackLayout>
                        </pcview:PancakeView>

                        <Image x:Name="AgentImage" Source="{Binding fullPortrait}" 
                            Margin="0,5,0,0"
                            HeightRequest="300"
                            HorizontalOptions="Center"
                            VerticalOptions="Start"
                            WidthRequest="300"/>
                        <Grid.GestureRecognizers>
                            <TapGestureRecognizer
                                Tapped="OnTapGestureRecognizerTapped"
                                NumberOfTapsRequired="1" />
                        </Grid.GestureRecognizers>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

the tap gets handled with the "OnTapGestureRecognizerTapped" function but I need the index of the tapped item and can’t figure out how.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The Tap handler:

async void OnTapGestureRecognizerTapped(object sender, EventArgs args)
    {
        await ExecuteNavigateToDetailPageCommand(Agents);
    }
   
    private async Task ExecuteNavigateToDetailPageCommand(ObservableCollection<Data> data)
    {
        await Navigation.PushAsync(new ValorantApp.Views.DetailPage(data));
    }

I’ve been searching everywhere and couldn’t find an answer to this.
What I want to do is, when an item in the CollectionView gets tapped I get the index of that item.

Any help is much appreciated. Thanks!

>Solution :

you can get the current row’s item from the BindingContext

async void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
    // I *think* the sender will be the Grid, but you 
    // might have to verify in the debugger.  If I'm 
    // wrong, cast to whatever the correct type is
    var grid = (Grid)sender;

    // item will be the Data corresponding to the row
    // you tapped.  If you actually need the index,
    // you can use IndexOf() to find it
    var item = (Data)grid.BindingContext;

    await ExecuteNavigateToDetailPageCommand(Agents);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading