How can we create checkboxs in Xamarin forms xamal?

I want a checkbox to select particular items from a list..Please send me the link or code where i can get it. Thank you

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    In XF you could use Switch. In xlabs I think should exists a checkbox control. Or you can use an Image with a TapGestureRecognizer

  • SheraShera INMember ✭✭

    @Rafeeq you have to create Custom Control CheckBox
    Try this :
    CheckBox.cs

    public class Checkbox : Button
        {
            public Checkbox()
            {
                base.Image = "Assets/newcbu.png";
                base.Clicked += new EventHandler(OnClicked);
                base.SizeChanged += new EventHandler(OnSizeChanged);
                base.BackgroundColor = Color.Transparent;
                base.BorderWidth = 0;
            }
    
            private void OnSizeChanged(object sender, EventArgs e)
            {
                //if (base.Height > 0)
                //{
                //    base.WidthRequest = base.Height;
                //}
            }
    
            public static BindableProperty CheckedProperty = BindableProperty.Create(
                propertyName: "Checked",
                returnType: typeof(Boolean?),
                declaringType: typeof(Checkbox),
                defaultValue: null,
                defaultBindingMode: BindingMode.TwoWay,
                propertyChanged: CheckedValueChanged);
    
            public Boolean? Checked
            {
                get
                {
                    if (GetValue(CheckedProperty) == null)
                    {
                        return null;
                    }
                    return (Boolean)GetValue(CheckedProperty);
                }
                set
                {
                    SetValue(CheckedProperty, value);
                    OnPropertyChanged();
                    RaiseCheckedChanged();
                }
            }
    
            private static void CheckedValueChanged(BindableObject bindable, object oldValue, object newValue)
            {
                if (newValue != null && (Boolean)newValue == true)
                {
                    ((Checkbox)bindable).Image = "Assets/newcbc.png";
                }
                else
                {
                    ((Checkbox)bindable).Image = "Assets/newcbu.png";
                }
            }
    
            public event EventHandler CheckedChanged;
            private void RaiseCheckedChanged()
            {
                if (CheckedChanged != null)
                    CheckedChanged(this, EventArgs.Empty);
            }
    
            private Boolean _IsEnabled = true;
            public Boolean IsEnabled
            {
                get
                {
                    return _IsEnabled;
                }
                set
                {
                    _IsEnabled = value;
                    OnPropertyChanged();
                    if (value == true)
                    {
                        this.Opacity = 1;
                    }
                    else
                    {
                        this.Opacity = .5;
                    }
                    base.IsEnabled = value;
                }
            }
    
            public void OnEnabled_Changed()
            {
    
            }
    
            public void OnClicked(object sender, EventArgs e)
            {
                Checked = !Checked;
    
                // Call the base class event invocation method.
                //base.Clicked(sender, e);
            }
    
        }
    

    Write this in Your Xaml namespace
    xmlns:controls="Your Projects Namespace"

    Xaml Code
    <controls:Checkbox x:Name="cb" Text="{Binding Name}" Checked="{Binding Selected}"/>

Sign In or Register to comment.