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.