Search code examples
xamlradio-buttonmauicollectionview

Correctly set RadioButtonGroup for CollectionView in .NET MAUI


In a .NET MAUI application, I'm using a CollectionView to display a particular ItemTemplate which also includes RadioButtons. However, the mutually exclusive state is not achieved, since somehow the Grid within the CollectionView is not recognized as the Parent for the RadioButtonGroup.

Here is the example the application: enter image description here

And here the corresponding XAML of the View:

<CollectionView ItemsSource="{Binding CurrentAnswers}"
                ItemsLayout="VerticalGrid"
                ItemSizingStrategy="MeasureAllItems"
                VerticalScrollBarVisibility="Always"
                HeightRequest="400"
                RadioButtonGroup.GroupName="Answers">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid x:DataType="models:Answer"
                  Padding="20">
                <Grid.RowDefinitions>
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <RadioButton Grid.Row="0"
                             Grid.Column="0"
                             Content="{x:Binding Content}"
                             TextColor="{StaticResource NightBlue}"
                             RadioButtonGroup.GroupName="Answers"
                             IsVisible="true" />
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

Solution

  • Try using GroupName instead of RadioButtonGroup.GroupName and set it only on RadioButtons (not on the parent).

    <RadioButton
        Grid.Row="0"
        Grid.Column="0"
        Content="{x:Binding Content}"
        TextColor="{StaticResource NightBlue}"
        GroupName="Answers"
        IsVisible="true" />