You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI:点击Label时切换CheckBox的选中状态

在.NET MAUI中实现点击Label切换CheckBox选中状态

当然可以实现,下面提供两种常用的方案:

方案1:使用数据绑定(推荐,符合MVVM模式)

这种方式适合复杂项目,将CheckBox的选中状态与ViewModel的属性绑定,通过Label的点击手势触发状态切换。

XAML代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyApp.Views"
             x:Class="MyApp.Views.Test"
             Title="Test">
    <ContentPage.BindingContext>
        <local:TestViewModel />
    </ContentPage.BindingContext>
    <HorizontalStackLayout>
        <CheckBox IsChecked="{Binding IsChecked}" />
        <Label Text="Check this box" VerticalOptions="Center">
            <Label.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding ToggleCheckCommand}" />
            </Label.GestureRecognizers>
        </Label>
    </HorizontalStackLayout>
</ContentPage>

ViewModel代码

using System.ComponentModel;
using System.Windows.Input;
using Microsoft.Maui.Controls;

namespace MyApp.Views
{
    public class TestViewModel : INotifyPropertyChanged
    {
        private bool _isChecked;
        public bool IsChecked
        {
            get => _isChecked;
            set
            {
                _isChecked = value;
                OnPropertyChanged(nameof(IsChecked));
            }
        }

        public ICommand ToggleCheckCommand { get; }

        public TestViewModel()
        {
            ToggleCheckCommand = new Command(() => IsChecked = !IsChecked);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

方案2:后台代码直接处理(简单场景适用)

如果是小型页面或快速原型,可直接在代码后台处理Label的点击事件,切换CheckBox状态。

XAML代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.Views.Test"
             Title="Test">
    <HorizontalStackLayout>
        <CheckBox x:Name="MyCheckBox" />
        <Label Text="Check this box" VerticalOptions="Center">
            <Label.GestureRecognizers>
                <TapGestureRecognizer Tapped="OnLabelTapped" />
            </Label.GestureRecognizers>
        </Label>
    </HorizontalStackLayout>
</ContentPage>

后台代码

namespace MyApp.Views
{
    public partial class Test : ContentPage
    {
        public Test()
        {
            InitializeComponent();
        }

        private void OnLabelTapped(object sender, EventArgs e)
        {
            MyCheckBox.IsChecked = !MyCheckBox.IsChecked;
        }
    }
}

内容的提问来源于stack exchange,提问作者Terra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 22:30:53