Xamarin. Radio button RadioButton.Content template dont support binding

Valentas M 21 Reputation points
2021-07-26T06:16:35.383+00:00

I have custom view radio button. Almost same as in MS example Binding value text dont work on Android.
If i write text instead {Binding Name} it appears in radio button content as expected.

<DataTemplate x:Key="RadioTemplate" x:DataType="dtoModels:PossibleAnswer">  
	<Frame Padding="10,5,10,5" CornerRadius="10" HasShadow="True">  
		<RadioButton GroupName="{Binding GroupId}" IsChecked="{Binding IsChecked}" Style="{StaticResource RadioButtonSurvey}">  
			<RadioButton.Content>  
				<Label Text="{Binding Name}"/>  
			</RadioButton.Content>  
		</RadioButton>  
	</Frame>  
</DataTemplate>  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-07-27T02:57:32.857+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If i write text instead {Binding Name} it appears in radio button content as expected

    This is because you use property-element syntax for the Content property of the RadioButton. The BindingContext of the radioButton and the 'Content' will be different, the 'Content''s BindingContext will be null. This is why the data binding of RadioButton.GroupName property works fine, but the Content not.

    Please use 'Content' property as the XML attribute instead. Change the code like below:

       <DataTemplate x:Key="RadioTemplate" x:DataType="dtoModels:PossibleAnswer">  
            <Frame Padding="10,5,10,5" CornerRadius="10" HasShadow="True">  
                <RadioButton GroupName="{Binding GroupId}" IsChecked="{Binding IsChecked}" Style="{StaticResource RadioButtonSurvey}" Content="{Binding Name}"/>  
            </Frame>  
        </DataTemplate>  
    

    Best Regards,

    Jarvan Zhang


    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.


0 additional answers

Sort by: Most helpful