NoWar
NoWar

Reputation: 37633

How to enable NonChecked checkbox in WPF

I am unchecking a disabled Checkbox using some code behind.

I need to enable this Checkbox when it is unchecked and do it using XAML.

I do this

 <CheckBox Margin="10,320,427,0" Name="chkIsDefault" 
IsEnabled="{Binding IsChecked, ElementName=Self, Mode=OneWay}" 
 IsChecked="{Binding IsDefault, Mode=TwoWay}"     />

But it is not working. Any clue?

Upvotes: 0

Views: 152

Answers (1)

dkozl
dkozl

Reputation: 33364

If you want to directly bind to another property of the same object use RelativeSource=Self

<CheckBox ... IsEnabled="{Binding IsChecked, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>

but since you want invert behaviour (disable when checked) you could modify binding to use custom IValueConverter but I would suggest to use a simple Trigger instead

<CheckBox IsChecked="{Binding IsDefault, Mode=TwoWay}">
    <CheckBox.Style>
        <Style TargetType="{x:Type CheckBox}">
            <Setter Property="IsEnabled" Value="True"/>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="IsEnabled" Value="False"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>

Upvotes: 1

Related Questions