question

AnasGuibene avatar image
0 Votes"
AnasGuibene asked AnasGuibene commented

How to get CollectionView SelectedItem value after using TapGestureRecognizer

Hello, I have this Collection View :

 <CollectionView  x:Name="lstDevices"
                                    Margin="20,0,20,10"
                                    SelectionMode="Single"
                                    ItemsSource="{Binding DevicesCollection,Mode=TwoWay}"
                                    >
                                 <CollectionView.ItemTemplate>
                                     <DataTemplate>
                                         <ContentView Padding="5">
                                             <Frame Style="{StaticResource FrameStyle}" Opacity="{Binding State,Converter={StaticResource StateToOpacityConverter}}" BackgroundColor="{Binding IsSelected,Converter={StaticResource BoolToColorConverter}}">
                                             <Frame.GestureRecognizers>
                                                 <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
                                             </Frame.GestureRecognizers>
                                                 <Grid Padding="10,5,0,0" BackgroundColor="{Binding IsSelected,Converter={StaticResource BoolToColorConverter}}">
                                                     <Grid.RowDefinitions>
                                                         <RowDefinition/>
                                                         <RowDefinition/>
                                                     </Grid.RowDefinitions>
                                                     <Grid.ColumnDefinitions>
                                                         <ColumnDefinition/>
                                                         <ColumnDefinition/>
                                                     </Grid.ColumnDefinitions>
    
                                                     <StackLayout Orientation="Horizontal" Spacing="5" Grid.Row="0" Grid.Column="0">
                                                         <Image Source="play.png"  Style="{StaticResource Key=StateImage}" Grid.Row="0" Grid.Column="0" IsVisible="{Binding IsRecording}"/>
                                                         <Image Source="pause.png"  Style="{StaticResource Key=StateImage}" Scale="0.75" Grid.Row="0" Grid.Column="0" IsVisible="{Binding IsStandingBy}"/>
                                                         <Image Source="powerOff.png"  Style="{StaticResource Key=StateImage}" Grid.Row="0" Grid.Column="0" IsVisible="{Binding IsDisconnected}"/>
                                                         <Image Source="stop.png"  Style="{StaticResource Key=StateImage}" Grid.Row="0" Grid.Column="0" IsVisible="{Binding IsWaiting}"/>
                                                         <Label Grid.Row="0" Grid.Column="0" Text="{Binding State , Converter={StaticResource StateToTradConverter}}" Margin="5" Style="{StaticResource StateLabelStyle}" TextColor="{Binding IsSelected,Converter={StaticResource BoolToTitleConverter}}"/>
                                                     </StackLayout>
    
                                                     <StackLayout Orientation="Horizontal" Spacing="5" Grid.Row="0" Grid.Column="1">
                                                         <abstractions:TintedImage Source="pictoMachine.png"  Style="{StaticResource ImageDevice}" TintColor="{Binding IsSelected,Converter={StaticResource TintedImageColorConverter}}"/>
                                                         <Label Text="{Binding MachineName}" FontFamily="Roboto-Regular" FontSize="14" TextColor="{Binding IsSelected,Converter={StaticResource BoolToUnselectionConverter}}" HorizontalOptions="Center" VerticalOptions="Center"/>
                                                     </StackLayout>
    
                                                     <StackLayout Orientation="Horizontal" Spacing="5" Grid.Row="1" Grid.Column="0">
                                                         <abstractions:TintedImage Source="pricetag.png" Style="{StaticResource ImageDevice}" TintColor="{Binding IsSelected,Converter={StaticResource TintedImageColorConverter}}"/>
                                                         <Label Text="{Binding DisplayName}" FontFamily="Roboto-Regular" FontSize="14" TextColor="{Binding IsSelected,Converter={StaticResource BoolToUnselectionConverter}}" HorizontalOptions="Center" VerticalOptions="Center"/>
                                                     </StackLayout>
    
                                                     <StackLayout Orientation="Horizontal" Spacing="5" Grid.Row="1" Grid.Column="1">
                                                         <abstractions:TintedImage Source="lastconnection.png"  Style="{StaticResource ImageDevice}" TintColor="{Binding IsSelected,Converter={StaticResource TintedImageColorConverter}}"/>
                                                         <Label  Text="{Binding LastConnectionDateUtc,StringFormat='{}{0:g}'}"  FontFamily="Roboto-Regular" FontSize="14" TextColor="{Binding IsSelected,Converter={StaticResource BoolToUnselectionConverter}}" HorizontalOptions="Center" VerticalOptions="Center"/>
                                                     </StackLayout>
                                                 </Grid>
                                             </Frame>
                                         </ContentView>
                                 </DataTemplate>
                                 </CollectionView.ItemTemplate>
                             </CollectionView>

How can I get the SelectedItem value after rasing the TapGestureRecognizer event?


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered AnasGuibene commented

Hello,​

Welcome to our Microsoft Q&A platform!

How can I get the SelectedItem value after rasing the TapGestureRecognizer event?

Try to get the SelectedItem value via the frame's bindingContext as below.

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
    var frame = sender as Frame;
    var model = frame.BindingContext as TestPageModel;

    //handle the data
}


Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.