UyerBit
UyerBit

Reputation: 71

How to change color of CheckBox when is not enabled?

I'm using CheckBox and I want to change the color to gray of the CheckBox that are disabled with IsEnabled = false .

Is posible to change the color in XAML? If is not possible, how can I make it possible in code?

My XAML:

            <Style x:Key="CheckBoxStyles" TargetType="CheckBox">
                <Setter Property="HorizontalOptions" Value="Center" />
                <Setter Property="VerticalOptions" Value="Center" />
                <Setter Property="Color" Value="{StaticResource checkBoxColor}" />
            </Style>
            
               <CheckBox
                                x:Name="FirstCB"
                                IsChecked="{Binding CheckedFirstCB, Mode=TwoWay}"
                                Style="{StaticResource CheckBoxStyles}" />
                              
               <CheckBox
                                x:Name="SecondCB"
                                IsChecked="{Binding CheckedSecondCB, Mode=TwoWay}"
                                Style="{StaticResource CheckBoxStyles}" />

Code:

FirstCB.IsEnabled = true;
SecondCB.IsEnabled = false;

Upvotes: 0

Views: 1363

Answers (1)

Cfun
Cfun

Reputation: 9671

As @RenéVogt mentioned in his comment you can achieve that with a style triggers:

 <Style x:Key="CheckBoxStyles" TargetType="CheckBox">
                <Setter Property="HorizontalOptions" Value="Center"/>
                <Setter Property="VerticalOptions" Value="Center"/>
                <Setter Property="Color" Value="{StaticResource checkBoxColor}"/>
                <Style.Triggers>
                    <Trigger TargetType="CheckBox" Property="IsEnabled" Value="False">
                        <Setter Property="Color" Value="Gray"/>
                    </Trigger>
                </Style.Triggers>
</Style>

Upvotes: 1

Related Questions