Setter and Trigger based on Property

Jassim Al Rahma 1,616 Reputation points
2022-03-18T19:25:52.657+00:00

Hi,

How can I use Trigger and Setter for a Button to change the BackgroundColor based on it's IsEnabled so if IsEnabled is True then the Background is Navy otherwise it's LightGray?

Thanks,
Jassim

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-03-21T02:18:44.82+00:00

    Hello,​

    How can I use Trigger and Setter for a Button to change the BackgroundColor based on it's IsEnabled so if IsEnabled is True then the Background is Navy otherwise it's LightGray?

    You can add two <Trigger> in <Button.Triggers>, when IsEnabled Property= true, set the background color to Navy. when IsEnabled Property= false, set the background color to LightGray.

       <Button Text="test" IsEnabled="False" >  
                   <Button.Triggers>  
                       <Trigger TargetType="Button"  
                        Property="IsEnabled" Value="True">  
                           <Setter Property="BackgroundColor" Value="Navy" />  
                            
                       </Trigger>  
                       <Trigger TargetType="Button"  
                        Property="IsEnabled" Value="False">  
                           <Setter Property="BackgroundColor" Value="LightGray" />  
         
                       </Trigger>  
                   </Button.Triggers>  
         
               </Button>  
    

    Here is a official article about the Xamarin.Forms Triggers, you can refer to it.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.