XAML .NET MAUI:单击标签时选中复选框

8ehkhllq  于 2022-12-07  发布在  .NET
关注(0)|答案(4)|浏览(231)

当单击复选框旁边的标签时,.NET MAUI中是否有一种方法可以切换复选框的状态?例如:

<?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 />
        <Label Text="Check this box" VerticalOptions="Center" />
    </HorizontalStackLayout>
</ContentPage>
sh7euo9m

sh7euo9m1#

基本上你需要这样做:

<ContentView
                Padding="10,20">
                <ContentView.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding SomeThingTappedCommand}" />
                </ContentView.GestureRecognizers>
                <StackLayout Orientation="Horizontal">
                    <Label
                        HorizontalOptions="StartAndExpand"
                        InputTransparent="True"
                        Text="Some Title"
                        VerticalTextAlignment="Center" />
                    <controls:ExtendedCheckBox
                        InputTransparent="True"
                        IsChecked="{Binding IsChecked}"
                        VerticalOptions="Center"
                        Color="{DynamicResource PrimaryColor}" />
                </StackLayout>
            </pan:PancakeView>

然后在您的虚拟机中,您只需:

SomeThingTappedCommand = new Command(() => IsChecked = !IsChecked);

其中IsChecked是一个应通知属性:

private bool isChecked;
 public bool IsChecked
    {
        get => isChecked;
        set => SetProperty(ref isChecked, value);
    }

古德勒克

6vl6ewon

6vl6ewon2#

在xamarin文件中:

<StackLayout Orientation="Horizontal">
        <CheckBox x:Name="myCheckBox"
              IsChecked="{Binding IsChecked,Mode=TwoWay}">
        </CheckBox>
        <Label VerticalTextAlignment="Center"  Text="Test">
            <Label.GestureRecognizers>
                <TapGestureRecognizer Tapped="Clicked"/>
            </Label.GestureRecognizers>
        </Label>
    </StackLayout>

在代码后面:

private void Clicked(object sender, EventArgs e)
    {
        myCheckBox.IsChecked = !myCheckBox.IsChecked;
    }
gmxoilav

gmxoilav3#

您可以像这样使用它,一个Label和一个CheckBox。
给予CheckBox命名。

<Label
            Text="Check this box"
            FontSize="32"
            HorizontalOptions="Center" >
            <Label.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"
                          NumberOfTapsRequired="1" />
            </Label.GestureRecognizers>
        </Label>
        
        <CheckBox x:Name="StackO" />

然后对于标签点击

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
    if (StackO.IsChecked )
       StackO.IsChecked = false;
    else
        StackO.IsChecked = true;
}
czfnxgou

czfnxgou4#

//xmal函数

<HorizontalStackLayout>
   <HorizontalStackLayout.GestureRecognizers>
     <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
   </HorizontalStackLayout.GestureRecognizers>
   <CheckBox IsChecked="True" VerticalOptions="Center" />
     <Label Text="Hello" VerticalOptions="Center" />
</HorizontalStackLayout>

//xaml.cs

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {
        var layout = sender as Layout;
        var check = layout.FirstOrDefault(c => c.GetType() == typeof(CheckBox));

        if (check != null)
        {
            ((CheckBox)check).IsChecked = !((CheckBox)check).IsChecked;
        }
    }

相关问题