question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Radiobutton with ControlTemplate doesn't recognize Click Event Correctly

See original GitHub issue

Description

When Radiobutton visual structure is redefined with a ControlTemplate, the click event is not recognized correctly by radio button: 1-by clicking inside Radiobutton, the the Radiobutton is not selected(Checked) 2-clicking outside the Radiobutton, change Radiobutton state to Selected(Checked) state.

LeftToRight-Outside-Click

Steps to Reproduce

1-Create a New MAUI Project 2-Add new Page 3-Code:(code is taken from Microsoft page @ Redefine RadioButton appearance)

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             FlowDirection="LeftToRight"
             x:Class="MauiApp29.Page1"
             IconImageSource="edit.png"
             Title="page1"
             BackgroundColor="GreenYellow"
             Padding="16">
    <ContentPage.Resources>
        <ControlTemplate x:Key="RadioButtonTemplate">
            <Frame BorderColor="#F3F2F1"
                   BackgroundColor="#F3F2F1"
                   HasShadow="False"
                   HeightRequest="100"
                   WidthRequest="100"
                   HorizontalOptions="Start"
                   VerticalOptions="Start"
                   Padding="0">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroupList>
                        <VisualStateGroup x:Name="CheckedStates">
                            <VisualState x:Name="Checked">
                                <VisualState.Setters>
                                    <Setter Property="BorderColor"
                                            Value="#FF3300" />
                                    <Setter TargetName="check"
                                            Property="Opacity"
                                            Value="1" />
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="Unchecked">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor"
                                            Value="#F3F2F1" />
                                    <Setter Property="BorderColor"
                                            Value="#F3F2F1" />
                                    <Setter TargetName="check"
                                            Property="Opacity"
                                            Value="0" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateGroupList>
                </VisualStateManager.VisualStateGroups>
                <Grid Margin="4"
                      WidthRequest="100">
                    <Grid WidthRequest="18"
                          HeightRequest="18"
                          HorizontalOptions="End"
                          VerticalOptions="Start">
                        <Ellipse Stroke="Blue"
                                 Fill="White"
                                 WidthRequest="16"
                                 HeightRequest="16"
                                 HorizontalOptions="Center"
                                 VerticalOptions="Center" />
                        <Ellipse x:Name="check"
                                 Fill="Blue"
                                 WidthRequest="8"
                                 HeightRequest="8"
                                 HorizontalOptions="Center"
                                 VerticalOptions="Center" />
                    </Grid>
                    <ContentPresenter />
                </Grid>
            </Frame>
        </ControlTemplate>

        <Style TargetType="RadioButton">
            <Setter Property="ControlTemplate"
                    Value="{StaticResource RadioButtonTemplate}" />
        </Style>
    </ContentPage.Resources>
    <ContentPage.Content>

        <StackLayout>
            <Label Text="What's your favorite mode of transport?" />
            <RadioButton Content="Car" />
            <RadioButton Content="Bike" />
            <RadioButton Content="Train" />
            <RadioButton Content="Walking" />
        </StackLayout>

    </ContentPage.Content>
</ContentPage>

Version with bug

Release Candidate 2 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 8-Android 9

Did you find any workaround?

No response

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:9
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mcpbcscommented, Oct 19, 2022

Hello, a workaround to solve is using a gesture like this:

<RadioButton Value="Cat" CheckedChanged="OnAnimalRadioButtonCheckedChanged">
            <RadioButton.Content>
                <StackLayout>
                    <Image Source="cat.png"
                           HorizontalOptions="Center"
                           VerticalOptions="Center">
                    </Image>
                    <Label Text="Cat"
                           HorizontalOptions="Center"
                           VerticalOptions="End" />
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="TapGestureRecognizer_Tapped"> 
                   </TapGestureRecognizer>
                    </StackLayout.GestureRecognizers>
                </StackLayout>
            </RadioButton.Content>
        </RadioButton>

And the code behind:

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
    RadioButton button = default;

    if (sender is Image)
    {
        button = ((Image)sender).Parent.Parent as RadioButton;
    }
    else if (sender is StackLayout)
    {
        button = ((StackLayout)sender).Parent as RadioButton;
    }
    else if (sender is Label)
    {
        button = ((Label)sender).Parent.Parent as RadioButton;
    }
    button.IsChecked = true;
}
2reactions
JozefGulacommented, Oct 1, 2022

I have the same problem on android devices.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Radio button control template binding "IsChecked" not ...
I have a control template like below, and I want to get IsChecked property when user selects a radio button. But when user...
Read more >
RadioButton ControlTemplate not displayed as Checked in ...
I have a Xamarin.Forms ControlTemplate for a RadioButton defined as follows: <Style x:Key="RadioButtonStyle" TargetType="RadioButton"> <Setter…
Read more >
RADIO BUTTONS in WPF? This is how it´s done - YouTube
The IsChecked property of a RadioButton can be set by clicking it, but it can ... For more information about creating a ControlTemplate,...
Read more >
Grid doesn't always refresh a column with a custom ...
Hi, I have a problem with some custom templates for grid cells. I created a template that shows a RadioButton to better visualize...
Read more >
Building a template-driven form
This tutorial shows you how to create a template-driven form. The control elements in the form are bound to data properties that have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found