How to style stroke color for MAUI RadioButton

Gjuro Kladaric 20 Reputation points
2024-02-11T02:12:45.92+00:00

RadioButton works as expected in MAUI, but when you run it on a dark themed screen, it works badly, as its two circles are displayed in blue on black, and that is a terrible combination. I don't know how to change that color, as StrokeColor is not accessible.

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2024-02-12T09:58:31.8633333+00:00

    Hello,

    Since “StrokeColor” property is only for Android native RadioButton, you mean Android platform, right?

    As described in the official RaidoButton doc: BorderColor, of type Color, which defines the border stroke color, you could try BorderColor. However, there is a known issue reporting at GitHub- RadioButton border doesn't work on Android · Issue #15795 · dotnet/maui (github.com), you could not consume this property, please follow the progress at GitHub.

    As a solution, you could redefine RadioButton appearance with a ControlTemplate, please refer to the following code:

      (The background color, Fill, Stroke properties respond to system theme changes. For more details, please refer to Respond to system theme changes - .NET MAUI | Microsoft Learn)

     <ContentPage.Resources>
            <ControlTemplate x:Key="RadioButtonTemplate">
                <Border Stroke="{AppThemeBinding Light=#F3F2F1, Dark=Cyan}"
                        StrokeThickness="2"
                        StrokeShape="RoundRectangle 10"
                        BackgroundColor="{AppThemeBinding Light=#F3F2F1, Dark=Cyan}"
                        HeightRequest="90"
                        WidthRequest="90"
                        HorizontalOptions="Start"
                        VerticalOptions="Start">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroupList>
                            <VisualStateGroup x:Name="CheckedStates">
                                <VisualState x:Name="Checked">
                                    <VisualState.Setters>
                                        <Setter Property="Stroke"
                                                Value="{AppThemeBinding Light=Green, Dark=Red}" />
                                        <Setter TargetName="ContentCheck"
                                                Property="Opacity"
                                                Value="1" />
                                    </VisualState.Setters>
                                </VisualState>
                                <VisualState x:Name="Unchecked">
                                    <VisualState.Setters>
                                        <Setter Property="BackgroundColor"
                                                Value="{AppThemeBinding Light=#F3F2F1, Dark=Cyan}"/>
                                        <Setter Property="Stroke"
                                                Value="{AppThemeBinding Light=#F3F2F1, Dark=Cyan}" />
                                        <Setter TargetName="ContentCheck"
                                                Property="Opacity"
                                                Value="0" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateGroupList>
                    </VisualStateManager.VisualStateGroups>
                    <Grid Margin="4"
                          WidthRequest="90">
                        <Grid Margin="0,0,4,0"
                              WidthRequest="18"
                              HeightRequest="18"
                              HorizontalOptions="End"
                              VerticalOptions="Start">
                            <Ellipse
                                Stroke="{AppThemeBinding Light=Green, Dark=Red}"
                                     Fill="{AppThemeBinding Light=Yellow, Dark=Purple}"
                                     WidthRequest="16"
                                     HeightRequest="16"
                                     HorizontalOptions="Center"
                                     VerticalOptions="Center" />
                            <Ellipse IsVisible="True"
                                x:Name="ContentCheck"
                                     Fill="{AppThemeBinding Light=Green, Dark=Red}"
                                     WidthRequest="8"
                                     HeightRequest="8"
                                     HorizontalOptions="Center"
                                     VerticalOptions="Center" />
                        </Grid>
                        <ContentPresenter />
                    </Grid>
                </Border>
            </ControlTemplate>
            <Style TargetType="RadioButton">
                <Setter Property="ControlTemplate"
                        Value="{StaticResource RadioButtonTemplate}" />
            </Style>
        </ContentPage.Resources>
    

    Best Regards,

    Wenyan Zhang


    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.

    1 person found this answer helpful.

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.