简体   繁体   中英

Xamarin Forms CollectionView Tap Gesture or Selection Changed not working

Xamarin.Forms my issue is atleast present on android.

I have a collectionview which is using grouping. I would like to make each collectionview group be clickable. I have tried adding a tapGesture recogniser to StackLayout in second data template, Label in top data template and the CollectionView itself, I have also tried them both as tapped and command. And lastly, I have tried SelectionChanged on the collectionView.

Clicking anywhere on the collectionview hits no breakpoints in ViewModel or View.cs

I would like it so clicking on an item will provide a parameter of sorts to the ViewModel so it knows what item in the CollectionView was hit

This is my XAML, if someone could see what blunder I am making that would be awesome! Thank you

<CollectionView Grid.Row="1"  ItemsSource="{Binding ResponseCollection}" SelectionMode="Single" SelectionChanged="ResponseCollectionView_SelectionChanged" x:Name="ResponseCollectionView" IsGrouped="True" >

                <CollectionView.GroupHeaderTemplate>
                    <DataTemplate >
                        <Label BackgroundColor="White" Padding="10,10,10,0" Text="{Binding Response}">
                        </Label>

                    </DataTemplate>
                </CollectionView.GroupHeaderTemplate>

                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <ScrollView>
                            <Grid Padding="10" BackgroundColor="White">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="1"/>
                                </Grid.RowDefinitions>

                                <StackLayout Grid.Row="0" Orientation="Horizontal">

                                    <Label TextColor="Red" Text="{Binding Statistic}"/>
                                    <Label TextColor="Red" Text="{Binding Amount}"/>
                                </StackLayout>
                                
                                <BoxView Grid.Row="1" BackgroundColor="Gray" HorizontalOptions="FillAndExpand"></BoxView>
                            </Grid>
                        </ScrollView>
                    </DataTemplate>
                </CollectionView.ItemTemplate>

            </CollectionView>

Use relative binding

  1. Add a name for the container (or ContentPage)
  2. Use relative binding for your tap command binding
<StackLayout x:Name="PageView">
    <CollectionView>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding Path=BindingContext.ViewModelCommand, Source={x:Reference PageView}}" />
                    </StackLayout.GestureRecognizers>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>

Updated to include the parameter:

<TapGestureRecognizer
    Command="{Binding Path=BindingContext.ViewModelCommand, Source={x:Reference PageView}}"
    CommandParameter="{Binding .}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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