vendredi 29 décembre 2017

How to use a CheckBox in Xamarin.forms?

I'm new in Xamarin.forms and I'm trying to use a multiple CheckBox that I create from a list.

I understand that CheckBox Doesn't exist in Xamarin.forms so I created a class that I finded on Internet to create the control.

When I try to create the CheckBox I can't see it. This is the code when I create the CheckBox:

if (List1 != null && List1.Count > 0)
{
    foreach (var c in List1)
    {
        CheckBox chk = new CheckBox();
        chk.CheckedChanged += Chk_CheckedChanged;
        chk.IsVisible = true;
        chk.CheckBoxBackgroundColor = Color.Blue;
        chk.TickColor = Color.Blue;
        chk.WidthRequest = 12;
        chk.HeightRequest = 12;
        StackLayoutBody.Children.Add(chk);
    }
}

And this is the code of the CheckBox.cs:

using System;
using Xamarin.Forms;

namespace TECAndroid.Services
{
    public class CheckBox : View
    {
        public static readonly BindableProperty CheckedProperty =
            BindableProperty.Create(nameof(Checked), typeof(bool), typeof(CheckBox), false, BindingMode.TwoWay,
                propertyChanged: (bindable, oldValue, newValue) =>
                {
                    ((CheckBox)bindable).CheckedChanged?.Invoke(bindable, new CheckedChangedEventArgs((bool)newValue));
                });


        public static readonly BindableProperty TickColorProperty =
            BindableProperty.Create(nameof(TickColor), typeof(Color), ypeof(CheckBox), Color.Default, BindingMode.TwoWay);

        public static readonly BindableProperty CheckBoxBackgroundColorProperty = BindableProperty.Create(nameof(CheckBoxBackgroundColor), typeof(Color), typeof(CheckBox), Color.Default, BindingMode.TwoWay);

        public EventHandler<CheckedChangedEventArgs> CheckedChanged;

        public Color TickColor
        {
            get => (Color)GetValue(TickColorProperty);
            set => SetValue(TickColorProperty, value);
        }

        public Color CheckBoxBackgroundColor
        {
            get => (Color)GetValue(CheckBoxBackgroundColorProperty);
            set => SetValue(CheckBoxBackgroundColorProperty, value);
        }

        public bool Checked
        {
            get => (bool)GetValue(CheckedProperty);
            set
            {
                if (Checked != value) SetValue(CheckedProperty, value);
            }
        }
    }

    public class CheckedChangedEventArgs : EventArgs
    {
        public CheckedChangedEventArgs(bool value)
        {
            Checked = value;
        }

        public bool Checked { get; }
    }
}

Can anyone help me please?!




Aucun commentaire:

Enregistrer un commentaire