Implement The Checkbox In MAUI Using Visual Studio 2022

Introduction

In this article, I will explain about MAUI checkbox implementation with label using visual studio 2022. Basically, checkbox is a type of button that can be checked or unchecked. If checkbox is checked value to be true or its false. Checkbox defines checked changed event that raised when the IsChecked property changes, either through user manipulation or when an application set IsChecked and CheckedChangedEventArgs object that can be the CheckedChanged event has a single property named Value, of type bool. When the event is raised, the value of the Value property is set to the new value of the IsChecked property.

CheckBox defines the following properties,

  • IsChecked, of type bool, which indicates whether the CheckBox is checked. This property has a default binding mode of Two-way.
  • Color, of type Colour, which indicates the colour of the CheckBox.

Step 1

Install visual studio 2022 with .NET core and .NET MAUI framework and follow the below screenshot.

Implement The Checkbox In MAUI Using Visual Studio 2022

Step 2

Create a new project in visual studio 2022 and select app option under the multiplatform on the left side and after that, you need to click .NET MAUI App with C# option and click continue button.

Implement The Checkbox In MAUI Using Visual Studio 2022

Step 3

On the next page, you need to select the .Net framework version 6.0 and click continue button.

Implement The Checkbox In MAUI Using Visual Studio 2022

Step 4

On the next page, you need to provide your project name and solution name along with location and click on create button

Implement The Checkbox In MAUI Using Visual Studio 2022

Step 5

After the created the MAUI project and you will navigate to the project structure with single code base and please refer the below screenshot.

Implement The Checkbox In MAUI Using Visual Studio 2022

Step 6

On the MainPage.xaml page you need to implement the check box with label and follow the below code help you to implement.

<?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="MAUI_CHECKBOX.MainPage">
  <Grid>
    <StackLayout Orientation="Horizontal" Padding="0" Margin="0">
      <AbsoluteLayout Margin="10">
        <Label Text="Green" Margin="20,0,0,0" />
        <CheckBox x:Name="checkBox" Color="Green" CheckedChanged="OnCheckBoxCheckedChanged" Margin="15" />
      </AbsoluteLayout>
      <AbsoluteLayout Margin="10">
        <Label Text="Orange" Margin="20,0,0,0" />
        <CheckBox x:Name="checkBox1" Color="Orange" CheckedChanged="OnCheckBoxCheckedChanged" Margin="15" />
      </AbsoluteLayout>
      <AbsoluteLayout Margin="10">
        <Label Text="red" Margin="20,0,0,0" />
        <CheckBox x:Name="checkBox2" Color="Red" CheckedChanged="OnCheckBoxCheckedChanged" Margin="15" />
      </AbsoluteLayout>
      <AbsoluteLayout Margin="10">
        <Label Text="Violet" Margin="20,0,0,0" />
        <CheckBox x:Name="checkBox3" Color="Violet" CheckedChanged="OnCheckBoxCheckedChanged" Margin="15" />
      </AbsoluteLayout>
    </StackLayout>
    <StackLayout Margin="30,80,30,0">
      <Label Text="Hey There, This is Manikandan and i'm here to help you to implement checkbox with lable">
        <Label.Triggers>
          <DataTrigger TargetType="Label"                      Binding="{Binding Source={x:Reference checkBox}, Path=IsChecked}" Value="true">
            <Setter Property="FontAttributes" Value="Italic, Bold" />
            <Setter Property="FontSize" Value="20" />
          </DataTrigger>
        </Label.Triggers>
      </Label>
    </StackLayout>
  </Grid>
</ContentPage>

Step 7

On the next step, you need to implement the checkbox event in mainpage.xaml.cs and please follow the below code.

void OnCheckBoxCheckedChanged(object sender, CheckedChangedEventArgs e) {
    // Perform required operation after examining e.Value
}

Output

Implement The Checkbox In MAUI Using Visual Studio 2022Implement The Checkbox In MAUI Using Visual Studio 2022

Conclusion

Hopefully, this article has given you sufficient information for you to create a MAUI app with checkbox. Feel free to leave a comment if you would like me to further elaborate on anything within this article