My question is an extension to this question where I want to change the text color based on the state of the radio button. I want it white if checked and black if unchecked.
Details:
I am using a custom styled Radio Button in my application following Redefine RadioButton appearance where it creates ControlTemplate to customize the appearance and uses ContentPresenter for displaying text/content.
I modified it to the shape I want, but unable to change the Text color based on the whether the radio button is checked or not.
Below is my code.
<ControlTemplate x:Key="RadioButtonTemplate">
<Border x:Name="RadioBorder1"
StrokeShape="RoundRectangle 3"
BackgroundColor="#F3F2F1"
HeightRequest="30"
WidthRequest="120">
<VisualStateManager.VisualStateGroups>
<VisualStateGroupList>
<VisualStateGroup x:Name="CheckedStates">
<VisualState x:Name="Checked">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="MediumPurple" />
<Setter Property="Stroke" Value="MediumPurple" />
<Setter TargetName="check" Property="Opacity" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unchecked">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="#F3F2F1" />
<Setter Property="Stroke" Value="#F3F2F1" />
<Setter TargetName="check" Property="Opacity" Value="0" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</VisualStateManager.VisualStateGroups>
<Grid Margin="4" WidthRequest="120">
<Grid WidthRequest="120" HeightRequest="30">
<RoundRectangle />
<RoundRectangle x:Name="check" />
</Grid>
<ContentPresenter HorizontalOptions="StartAndExpand" Padding="5,1,1,1" />
</Grid>
</Border>
</ControlTemplate>
<Style TargetType="RadioButton">
<Setter Property="ControlTemplate"
Value="{StaticResource RadioButtonTemplate}" />
</Style>
And this is what I have now. and I am trying to change the text color to white for the selected item.
Thanks for your help.
srk_fei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.