We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Event Binding in Viewmodel for Checkbox

Thread ID:

Created:

Updated:

Platform:

Replies:

146601 Aug 9,2019 06:09 PM UTC Aug 13,2019 10:56 AM UTC Xamarin.Forms 1
loading
Tags: SfCheckBox
Matthew Bailey
Asked On August 9, 2019 06:09 PM UTC

I have a pair of Checkboxes that need their respective values for IsChecked to be opposite.

CheckBoxA = IsChecked ~~~ CheckBoxB = !IsChecked

They should toggle eachother, and I had that managed with Xamarin's standard Checkbox object by attaching Triggers, but when I try to write a Trigger on SfCheckBox Visual Studio gives me an error that it doesn't exist.

Can someone help me figure out how to set triggers or utilize StateChanged through a ViewModel Binding?

Rachel A [Syncfusion]
Replied On August 13, 2019 10:56 AM UTC

Hi Matthew, 
  
Greetings from Syncfusion. 
 
We have prepared sample to achieve your requirement by using view model property binding and Event Triggers. The sample can be downloaded from the below location. 
  
  
Solution 1: Using the MVVM pattern (Property Binding) 
[XAML] 
  <sfButtons:SfCheckBox x:Name="CheckBox1" Text="Combo 1" IsChecked="{Binding IsCheckBox1Checked}" Grid.Row="1" Grid.Column="0" Margin="10,0,20,0"/> 
  <sfButtons:SfCheckBox x:Name="CheckBox2" Text="Combo 2" IsChecked="{Binding IsCheckBox2Checked}" Grid.Row="1" Grid.Column="1" Margin="20,0,10,0"/> 
  
Change the checked value based on another checked property value like below: 
[C#] 
        private bool chk_CheckBox1; 
        private bool chk_CheckBox2; 
        public bool IsCheckBox1Checked 
        { 
            get { return chk_CheckBox1; } 
            set 
            { 
                chk_CheckBox1 = value; 
                if (chk_CheckBox1) 
                    IsCheckBox2Checked = false; 
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsCheckBox1Checked")); 
            } 
        } 
        public bool IsCheckBox2Checked 
        { 
            get { return chk_CheckBox2; } 
            set 
            { 
                chk_CheckBox2 = value; 
                if (chk_CheckBox2) 
                    IsCheckBox1Checked = false; 
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsCheckBox2Checked")); 
            } 
        } 
  
Solution 2:  Event Triggers 
[XAML] 
                <sfButtons:SfCheckBox x:Name="CheckBox3" Text="Combo 3" Grid.Row="3" Grid.Column="0" Margin="10,0,20,0"> 
                    <sfButtons:SfCheckBox.Triggers> 
                        <EventTrigger Event="StateChanged"> 
                            <local:CheckStateValidationTriggerAction OpponentCheckBox="{x:Reference CheckBox4}"/> 
                        </EventTrigger> 
                    </sfButtons:SfCheckBox.Triggers> 
                </sfButtons:SfCheckBox> 
                <sfButtons:SfCheckBox x:Name="CheckBox4" Text="Combo 4" Grid.Row="3" Grid.Column="1" Margin="20,0,10,0"> 
                    <sfButtons:SfCheckBox.Triggers> 
                        <EventTrigger Event="StateChanged"> 
                            <local:CheckStateValidationTriggerAction OpponentCheckBox="{x:Reference CheckBox3}"/> 
                        </EventTrigger> 
                    </sfButtons:SfCheckBox.Triggers> 
                </sfButtons:SfCheckBox> 
  
Changing opponent checkbox state based on triggered check box state.  
[C#] 
  public class CheckStateValidationTriggerAction : TriggerAction<SfCheckBox> 
    { 
       public SfCheckBox OpponentCheckBox { get; set; } 
        protected override void Invoke(SfCheckBox checkbox) 
        { 
           if(OpponentCheckBox != null) 
            { 
                OpponentCheckBox.IsChecked = !checkbox.IsChecked; 
            } 
        } 
    } 
  
Regards, 
Rachel. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon