Innova
Innova

Reputation: 396

Xamarin.Forms: Tapped event on CheckBox control

Is there a way to recognize something like Tapped event on the CheckBox control? CheckedChanged event is also triggered at the creation of the container (in my case a CollectionView) and at the scroll too, instead I need to manage the change of state only at the user's tap. I'm trying to use GestureRecognizers, but the command is fired only if I tap on StackLayout. If I tap on CheckBox nothing happens.

    <CollectionView 
        x:Name="LvSospesi" 
        ItemsSource="{Binding ListaSospesi}"           
        SelectedItem="{Binding SospesoModelCorrente, Mode=TwoWay}">
        
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="model:SospesoModel">
                <Grid 
                    IsEnabled="{Binding BindingContext.ListaSospesiEnabled, Source={x:Reference RootPage}}">
                                            
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="40"/>      
                        <ColumnDefinition Width="*"/>      
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="{StaticResource AltezzaRigheGriglieArticoli}"/>
                    </Grid.RowDefinitions>

                    <StackLayout 
                        Grid.Column="0" 
                        Grid.Row="0">

                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding BindingContext.CheckSospesoCommand, Source={x:Reference RootPage}}"
                                                  CommandParameter="{Binding .}"/>
                        </StackLayout.GestureRecognizers>
                        
                        <CheckBox 
                            InputTransparent="True"
                            IsChecked="{Binding Selezionato}">                                
                        </CheckBox>
                    </StackLayout>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

Upvotes: 0

Views: 664

Answers (2)

FreakyAli
FreakyAli

Reputation: 16459

Make the input of your checkbox transparent using InputTransparent and Wrap it in a Stack or some other layout and get the events from your Layout

<StackLayout>  <!--This will now take your checkbox click events and you can wrap it under other layouts-->
    <StackLayout.GestureRecognizers>    
          <TapGestureRecognizer
            Command="{Binding CheckboxTappedCommand}"
            />
    </StackLayout.GestureRecognizers>    
  <CheckBox InputTransparent= "True" IsChecked="{Binding IsChecked}" />
</StackLayout>

Upvotes: 1

Dharmendar Dhanasekar
Dharmendar Dhanasekar

Reputation: 211

Tap gesture will not directly work on checkbox, even if input transparent property is set to true. You have to place checkbox inside grid and place some box view with transparent background above checkbox and listen tapped event/command from box view

            <Grid>
                <CheckBox
                    HorizontalOptions="StartAndExpand"
                    InputTransparent="True"/>
                <BoxView
                    Color="Transparent">
                    <BoxView.GestureRecognizers>
                        <TapGestureRecognizer Tapped="OnCheck_Changed"/>
                    </BoxView.GestureRecognizers>
                </BoxView>
            <Grid>

Upvotes: 0

Related Questions