简体   繁体   中英

Checkbox is not clickable | Xamarin.forms

Basically, I have a checkbox in a content page, and I can't click it. It shows it correctly, but when I click on it, nothing happens. It doesn't even check the checkbox, nothing happens as if its an image.

I've tried:

IsEnabled="True"
IsChecked="True"

This is one of my 3 checkboxes:

<CheckBox x:Name="cbop"
          Grid.Row="1"
          CheckedChanged="Cbop_CheckedChanged"
          IsEnabled="True"
          Grid.Column="3" />

This is the cbop_CheckedChange:

        private void Cbop_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            cbhardcore.IsChecked = false;
            cbnormal.IsChecked = false;
        }

Full code of xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App3.Views.GameSettings">
    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="120" />
                <RowDefinition Height="120" />
                <RowDefinition Height="120" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="25*" />
                <ColumnDefinition Width="20*" />
                <ColumnDefinition Width="20*" />
                <ColumnDefinition Width="23*" />
                <ColumnDefinition Width="10*" />
            </Grid.ColumnDefinitions>
            <CheckBox x:Name="cbhardcore"
                      Grid.Row="1"
                      CheckedChanged="Cbhardcore_CheckedChanged"
                      IsEnabled="True"
                      Grid.Column="1" />
            <CheckBox x:Name="cbnormal"
                      Grid.Row="1"
                      CheckedChanged="Cbnormal_CheckedChanged"
                      IsEnabled="True"
                      Grid.Column="2" />
            <CheckBox x:Name="cbop"
                      Grid.Row="1"
                      CheckedChanged="Cbop_CheckedChanged"
                      IsEnabled="True"
                      Grid.Column="3" />
            <Label x:Name="lblharcore"
                   Grid.Row="1"
                   Grid.Column="1"
                   Text="Hardcore" />
            <Label x:Name="lblnormal"
                   Grid.Row="1"
                   Grid.Column="2"
                   Text="Normal" />
            <Label x:Name="lblop"
                   Grid.Row="1"
                   Grid.Column="3"
                   Text="Overpowered" />
        </Grid>
    </ContentPage.Content>
</ContentPage>

Full code of behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App3.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class GameSettings : ContentPage
    {
        public GameSettings()
        {
            InitializeComponent();
        }

        private void Cbop_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            cbhardcore.IsChecked = false;
            cbnormal.IsChecked = false;
        }

        private void Cbnormal_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            cbhardcore.IsChecked = false;
            cbop.IsChecked = false;
        }

        private void Cbhardcore_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            cbop.IsChecked = false;
            cbnormal.IsChecked = false;
        }
    }
}

Cause: You put the Label and CheckBox in the same cell of grid(the same row and column). So the checkbox will be covered by the label .You can set the BackgroundColor of Label to check it .

<Label x:Name="lblharcore"
               BackgroundColor="red"
               Grid.Row="0"
               Grid.Column="1"
               Text="Hardcore" />

Solution: Improve your layout.

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="120" />
            <RowDefinition Height="120" />
            <RowDefinition Height="120" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="23*" />
            <ColumnDefinition Width="10*" />
        </Grid.ColumnDefinitions>

        <StackLayout Grid.Column="1" Grid.Row="1" VerticalOptions="Center" HorizontalOptions="Center">

            <Label x:Name="lblharcore"
                   BackgroundColor="red"                  
                   Text="Hardcore" />
            <CheckBox x:Name="cbhardcore"                     
                      CheckedChanged="Cbhardcore_CheckedChanged"
                      IsEnabled="True"
                       />
        </StackLayout>


        <StackLayout Grid.Column="2" Grid.Row="1" VerticalOptions="Center" HorizontalOptions="Center">
            <Label x:Name="lblnormal"                 
                   Text="Normal" />

            <CheckBox x:Name="cbnormal"                      
                      CheckedChanged="Cbnormal_CheckedChanged"
                      IsEnabled="True"
                       />
        </StackLayout>

        <StackLayout Grid.Column="3" Grid.Row="1" VerticalOptions="Center" HorizontalOptions="Center">
            <Label x:Name="lblop"                 
                   Text="Overpowered" />

            <CheckBox x:Name="cbop"                    
                      CheckedChanged="Cbop_CheckedChanged"
                      IsEnabled="True"
                       />

        </StackLayout>
</Grid>

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