xamarin-forms-docs/knowledge-base/checkbox-change-check-order.md

2.9 KiB

title description type page_title slug position tags ticketid res_type
Order the CheckBox states how to change the checkbox states order how-to change the checkbox state order checkbox-change-check-order checkbox, state, ischecked, indeterminate, unchecked, checked, xamarin, xamarin.forms 1444495 kb

Environment

Product Version 2019.3 1119.1
Product Checkbox for Xamarin

Description

By default the logic of the RadCheckBox IsChecked state when IsThreeState = "True" is Indeterminate -> Unchecked -> Checked. For sure there will be some scenarios when you want to change the state flow, for example Indeterminate -> Checked -> Unchecked.

In this help article we will show you how this could be achieved.

Solution

The CheckBox IsChecked state flow can be changed inside the RadCheckBox IsCheckedChanged event.

  1. Create a ViewModel class with property that will be bind to the CheckBox IsChecked property.

    public class ViewModel : NotifyPropertyChangedBase
    {
        private bool? myproperty;
        public bool? MyProperty 
        {
            get
            {
                return this.myproperty;
            }
            set
            {
                if(this.myproperty != value)
                {
                    this.myproperty = value;
                    OnPropertyChanged();
                }
            }
        }
    }
    
  2. then inside the code behind create a bool property that will be used to trigger whether the checkbox state is changed and inside the RadCheckBox_IscheckedChanged event implement a custom logic for changing the state:

    public partial class MainPage : ContentPage
    {
        ViewModel vm;
    
    
    
        public MainPage()
        {
            InitializeComponent();
    
            this.vm = new ViewModel();
            this.BindingContext = vm;
        }
        private bool isinternalchanged = true;
        public bool IsInternalChanged
        {
            get
            {
                return this.isinternalchanged;
            }
    
            set
            {
                if (this.isinternalchanged)
                {
                    return;
                }
            }
        }
        private void RadCheckBox_IsCheckedChanged(object sender, Telerik.XamarinForms.Primitives.CheckBox.IsCheckedChangedEventArgs e)
        {
            var obj = sender as RadCheckBox;
            var old = e.OldValue;
    
            if (this.isinternalchanged)
            {
                this.isinternalchanged = false;
                return;
            }
    
            if(old == null)
            {
                this.isinternalchanged = true;
                vm.MyProperty = true;
            }
    
            else if(old == true)
            {
                this.isinternalchanged = true;
                vm.MyProperty = false;
            }
    
            else if(old == false)
            {
                this.isinternalchanged = true;
                vm.MyProperty = null;
            }
        }
    }