1
votes

I would like to enable a button when either of the 2 check boxes are checked. When neither of the check boxes are checked, the button should be inactive(IsEnabled = false)

It is possible to bind like this.

IsEnabled="{Binding ElementName=CheckBox Path=IsChecked}"

But it works only for a single checkbox. I want to bind both the check boxes IsChecked properties to the IsEnabled property of button in XAML itself. (I know how to do using property changed in code)

I tried using Multi triggers.

            <Button.IsEnabled>
                <MultiBinding>
                    <MultiBinding.Bindings>                                                  <Binding ElementName="BlankmeasurementCheckBox" Path="IsChecked"/>
                     <Binding ElementName="MeasurementCheckBox" Path="IsChecked"/>
                  </MultiBinding.Bindings>
             </MultiBinding>
          </Button.IsEnabled>

But it doesn't seem to help. Could you please help me out? Thanks in advance.

1
please post what have you tried....Abhinav Sharma
the above code you posted will only set for one checkbox.Abhinav Sharma
i have added.. please checkViVi
are you following mvvm pattern (is there any viewmodel or model, command etc.)Abhinav Sharma
What you can do is,you can define two bool properties and bind it to the two checkboxes,then you can set the isEnabled on the canexecute method of the button`s command based on the properties.Abhinav Sharma

1 Answers

3
votes

You can make use of MultiDataTrigger here.

Here is the sample code:

 <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <CheckBox Name="cbSampleYes" Content="Yes" />
        <CheckBox Name="cbSampleSure" Content="I'm sure" />
        <Button HorizontalAlignment="Center" Margin="0,20,0,0">
            <Button.Style>
                <Style TargetType="Button">
                    <Setter Property="Content" Value="Verified" />                    
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=cbSampleYes, Path=IsChecked}" Value="False" />
                                <Condition Binding="{Binding ElementName=cbSampleSure, Path=IsChecked}" Value="False" />
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Content" Value="Unverified" />
                            <Setter Property="IsEnabled" Value="False" />
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
    </StackPanel>