Extra Whitespace Added When Including Multiple RadioButtons

Nathan Sokalski 4,116 Reputation points
2021-01-26T03:46:41.88+00:00

I have a RadioButton Style & ControlTemplate, and when using them for more than one RadioButton, a large amount of whitespace is added above & below the containing Grid. Here is the design part of the ControlTemplate:

<Grid BackgroundColor="Transparent" Padding="0" HorizontalOptions="Fill" Margin="0" VerticalOptions="Fill" ColumnSpacing="5">  
	<Grid.RowDefinitions><RowDefinition Height="*"/></Grid.RowDefinitions>  
	<Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions>  
	<Grid BackgroundColor="Transparent" Padding="0" Margin="0" HorizontalOptions="Fill" VerticalOptions="Fill">  
		<Ellipse Stroke="Black" WidthRequest="24" HeightRequest="24" StrokeThickness="2" VerticalOptions="Center" HorizontalOptions="Center" Fill="Transparent"/>  
		<Ellipse x:Name="Check" WidthRequest="14" HeightRequest="14" Fill="Black" VerticalOptions="Center" HorizontalOptions="Center"/>  
	</Grid>  
	<ContentPresenter Grid.Column="1" BackgroundColor="Transparent" HorizontalOptions="Fill" Margin="0" Padding="0" VerticalOptions="Fill"/>  
</Grid>  

When there is only one RadioButton, it looks as I would expect. However, if there are multiple RadioButton(s), extra whitespace gets added above & below the Grid. Here are screenshots of the Grid with three & one RadioButton:
60378-screenshot-2021-01-24-175450.png 60491-screenshot-2021-01-24-175842.png
I found that if I remove either the Grid containing the Ellipse(s) or the ContentPresenter from the ControlTemplate (either one, it just won't let me have both) it works fine, but if both are there having multiple RadioButtons causes extra whitespace above & below the Grid containing the RadioButtons. Why is this space being added when there are multiple RadioButton(s)? Thanks.

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

Accepted answer
  1. Nathan Sokalski 4,116 Reputation points
    2021-02-08T16:51:30.583+00:00

    I don't know WHY it solved the problem (since the problem was vertical and my solution set the WidthRequest, which is a horizontal property), but when I add a WidthRequest to each RadioButton it stopped showing the unexplained whitespace. In order to make this dynamic (not needing to manually calculate the width for a literal value), I used Binding, such as in the following (in this example the RadioButton is radBoth):

    WidthRequest="{Binding Width,Source={x:Reference radBoth}}"
    

    So I think there is a bug in the XAML Layout controls, but hopefully this can help others as a workaround, since it drove me crazy for weeks!

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-01-26T09:10:39.667+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I test with your code , it works fine without extra whitespace .
    60586-capture.png

    Could you post the code used in xaml ? We need to reproduce the issue first for further troubleshooting.

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    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.


  2. mc 3,701 Reputation points
    2021-01-28T02:18:42.943+00:00

    The Grid has a default spacing you can set it to 0.