L. Page
L. Page

Reputation: 168

How to enable a control if a CheckBox is Unchecked?

I know that is possible disable a Control if the CheckBox is not checked, generally I did this:

<CheckBox x:Name="myCheckBox" />
<Button IsEnabled="{Binding ElementName=myCheckBox, Path=IsChecked}" />

this will enable the Button only if the CheckBox is checked, but there is a way in Xaml to enable the Button if the CheckBox is unchecked without create any Converter?

Pseudo code:

<CheckBox x:Name="myCheckBox" />
<Button IsEnabled="{Binding ElementName=myCheckBox, Path=IsUnChecked}" />

Best regards.

Upvotes: 1

Views: 2081

Answers (2)

Nikhil G
Nikhil G

Reputation: 1594

Putting this answer to update visitors about the new syntax of DataTrigger to achieve the same. It worked for me on "Visual Studio 2015" or later.

        <CheckBox x:Name="myCheckBox"/>
        <Button x:Name="button">
            <Button.Style>
                <Style TargetType="Button">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=myCheckBox, Path=IsChecked}"
                                     Value="True">
                            <DataTrigger.Setters>
    <!--it would make the Button in Disabled state since checkbox is in Checked state-->
                              <Setter Property="IsEnabled" Value="False"/> 
                        </DataTrigger.Setters>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=myCheckBox, Path=IsChecked}"
                                 Value="False">
                        <DataTrigger.Setters>
<!--it would make the Button in Enabled state since checkbox is in Unchecked state-->
                              <Setter Property="IsEnabled" Value="True"/>
                        </DataTrigger.Setters>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

I hope it helps all. Thanks!

Upvotes: -1

Clemens
Clemens

Reputation: 128157

You may use the Binding with a DataTrigger:

<CheckBox x:Name="myCheckBox"/>
<Button x:Name="button">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=myCheckBox}"
                             Value="True">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

Upvotes: 6

Related Questions