Learn About XF Checkbox

Hello guys,

I know that creating a beautiful app requires attractive widgets or controls. If we want to make a beautiful app, we'll need enough widgets or controls. And you also know very well that Xamarin does not have enough widgets, such as checkboxes, radio button groups etc.

So, guys, I have come here to make the checkbox control for you.

Xamarin

A checkbox is a selection box or a tick box which is required to a form or window and page, which we can answer by selecting an option for an answer to the question. Or you can use this to select the setting.

So, let's start.

Xamarin

Implementation

  • Go to PCL project and right-click on the project.
  • And then, select Add and click on New items.
  • Here a dialog box appears. Here, I am choosing content view and then giving the name "Checkbox.xaml", click OK.
  • Creating ”Checkbox.xaml” content View, we design a View for a custom Checkbox.

XAML CODE=> Checkbox.xaml

  1. <Grid ColumnSpacing="5" HeightRequest="24">  
  2.     <Grid.ColumnDefinitions>  
  3.         <ColumnDefinition Width="Auto" />  
  4.         <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions>  
  5.     <Image x:Name="MainImage" Grid.Column="0" Source="chk" HeightRequest="16" WidthRequest="16" Aspect="AspectFit" />  
  6.     <Label x:Name="MainLabel" Grid.Column="1" Text="{Binding Text,Mode=TwoWay}" FontSize="Small" TextColor="{Binding TextColor,Mode=TwoWay}" />   
  7. </Grid>  

Okay, now, I am satisfied with this design so we are setting the properties of text - TextColor, and IsChecked Properties.

Here, we will write some code like below.

C# Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7. using Xamarin.Forms.Xaml;  
  8. namespace XamBuddyApp.CustomControls.CustomView {  
  9.     [XamlCompilation(XamlCompilationOptions.Compile)]  
  10.     public partial class CheckBox: ContentView {  
  11.         public CheckBox() {  
  12.             InitializeComponent();  
  13.             MainLabel.BindingContext = this;  
  14.             IsChecked = false;  
  15.             var tgr = new TapGestureRecognizer {  
  16.                 NumberOfTapsRequired = 1  
  17.             };  
  18.             tgr.Tapped += ViewOn_Clicked;  
  19.             this.GestureRecognizers.Add(tgr);  
  20.         }  
  21.         private void ViewOn_Clicked(object sender, EventArgs e) {  
  22.             Device.BeginInvokeOnMainThread(async () => {  
  23.                 if (check == true) {  
  24.                     await Task.WhenAll(MainImage.FadeTo(0, 100), MainImage.ScaleTo(0, 100), MainImage.TranslateTo(0, 0, 100, Easing.BounceIn));  
  25.                     MainImage.Source = "uncheck";  
  26.                     check = false;  
  27.                     await Task.WhenAll(MainImage.FadeTo(1, 100), MainImage.ScaleTo(1, 100), MainImage.TranslateTo(0, 0, 100, Easing.BounceIn));  
  28.                 } else {  
  29.                     await Task.WhenAll(MainImage.FadeTo(0, 100), MainImage.ScaleTo(0, 100), MainImage.TranslateTo(0, 0, 100, Easing.BounceIn));  
  30.                     MainImage.Source = "check";  
  31.                     check = true;  
  32.                     await Task.WhenAll(MainImage.FadeTo(1, 100), MainImage.ScaleTo(1, 100), MainImage.TranslateTo(0, 0, 100, Easing.BounceIn));  
  33.                 }  
  34.             });  
  35.         }  
  36.         public static BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(CheckBox), defaultBindingMode: BindingMode.TwoWay);  
  37.         public static BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(CheckBox), defaultBindingMode: BindingMode.TwoWay, defaultValue: Color.Black);  
  38.         public static BindableProperty IsCheckedProperty = BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(CheckBox), defaultValue: false, propertyChanged: (bindable, oldVal, newVal) => {  
  39.             var matChk = (CheckBox) bindable;  
  40.             matChk.check = (bool) newVal;  
  41.             if (matChk.IsChecked == true) matChk.MainImage.Source = "check";  
  42.             else matChk.MainImage.Source = "uncheck";  
  43.         });  
  44.         private bool check = false;  
  45.         public bool IsChecked {  
  46.             get {  
  47.                 return check;  
  48.             }  
  49.             set {  
  50.                 if (IsChecked == true) {  
  51.                     MainImage.Source = "check";  
  52.                 } else {  
  53.                     MainImage.Source = "uncheck";  
  54.                 }  
  55.             }  
  56.         }  
  57.         public string Text {  
  58.             get => (string) GetValue(TextProperty);  
  59.             set => SetValue(TextProperty, value);  
  60.         }  
  61.         public Color TextColor {  
  62.             get => (Color) GetValue(TextColorProperty);  
  63.             set => SetValue(TextColorProperty, value);  
  64.         }  
  65.     }  
  66. }  

After XAML and C# Code, finally, we use CheckBox in Xamarin.Forms.

Here is the demo code for testing the CheckBox.

  1. <StackLayout BackgroundColor="#384E70" Padding="30" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">  
  2.     <chk:CheckBox x:Name="chk" IsChecked="False" Text="This is checkbox" TextColor="Red" />  
  3.     <Button x:Name="btntestchk" Text="Test Check Box" Style="{StaticResource buttonStyle}" Clicked="btntestchk_Clicked" />   
  4. </StackLayout>  

And ButtonClick Code

  1. private void btntestchk_Clicked(object sender, EventArgs e) {  
  2.     if (chk.IsChecked == true) DisplayAlert("Info""Check ""OK");  
  3.     else if (chk.IsChecked == false) DisplayAlert("Info"" Uncheck ""OK");  
  4. }  

Xamarin

If you want to see this on my blog, click here and watch my video.