Enabled Style in App.xaml

Jassim Al Rahma 1,556 Reputation points
2022-03-18T16:42:51.463+00:00

Hi,

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

Thanks,
Jassim

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2022-03-21T02:01:40.587+00:00

    Hello,​

    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:

    "Normal"
    "Disabled"
    "Focused"
    "Selected"

    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).

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

    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