Enabled Style in App.xaml

Jassim Al Rahma 1,526 Reputation points


How can I set the the color of the button when it's Enabled and when it's NOT enabled inside the App.xaml so that I don't need to to do it in every ContentPage?

For example, I want to use Navy when IsEnabled = true and LightGray when IsEnabled = false


A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,386 Reputation points Microsoft Vendor


    I want to use Navy when IsEnabled = true and LightGray when IsEnabled = false

    You can try to use Xamarin.Forms Visual State Manager and Global Styles to achieve it.

    Visual State Manager defines one visual state group named "CommonStates" with the following visual states:


    You can use Normal and Disabled states, then set different color in these two states like following styles in App.xaml(Note: I do not know which color of property that you want to be changed, I set the TextColor for testing. If you want to change the background color, you can change TextColor to BackgroundColor).

               <Style TargetType="Button"  x:Key="buttonStyle"  
                   <Setter Property="VisualStateManager.VisualStateGroups">  
                       <VisualStateGroupList x:Name="CommonStates">  
                               <VisualState x:Name="Normal">  
                                       <Setter Property="TextColor" Value="Navy"/>  
                               <VisualState x:Name="Disabled">  
                                       <Setter Property="TextColor" Value="LightGray"/>  

    Then you can use this style in your Button.

       <Button Text="test" Style="{StaticResource buttonStyle}" ></Button>  

    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