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. Image for the cookie policy date

Event Binding in Viewmodel for Checkbox

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?

1 Reply

RA Rachel A Syncfusion Team 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. 


Loader.
Live Chat Icon For mobile
Up arrow icon