question

ValentasM-2059 avatar image
0 Votes"
ValentasM-2059 asked ValentasM-2059 commented

Xamarin. Radio button RadioButton.Content template dont support binding

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>



dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered ValentasM-2059 commented

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.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I just simplified my example, but it is more complicated. My use case is to populate radio buttons dynamically (Survey answers) with their custom style (like in ms example).
So could you confirm that this is impossible?
I want dynamicaly bind image or text.

 <RadioButton>
     <RadioButton.Content>
         <Image Source="{Binding Image}" />
     </RadioButton.Content>
 </RadioButton>


0 Votes 0 ·

You could try using a template selector to display the different styles for the view.

Check the doc:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/selector

0 Votes 0 ·

There is no such property as ItemTemplate in Radio Button. So i see that there is no easy solution. Closing

0 Votes 0 ·