Search code examples
xamlmvvmxamarin.formsxlabs

How to use Checked binding with radio button on item selected of the listview cell with Xamarin Forms?


I am using radio button with listview and on listview item click I want to make radio button checked/selected but unfortunately not able to select the radio button.

                       <StackLayout  VerticalOptions="CenterAndExpand" HeightRequest="200" HorizontalOptions="CenterAndExpand" >
                        <ListView RowHeight="45" IsVisible="true" ItemsSource="{Binding Items}" BackgroundColor="#5679d1"
                                  SelectedItem="{Binding objItemSelected, Mode=TwoWay}" 
                                  HasUnevenRows="true" SeparatorVisibility="Default" SeparatorColor="White">
                            <ListView.ItemTemplate>  
                                <DataTemplate>  
                                    <ViewCell>

                                            <Grid>
                                                <Label Text="{Binding Questions}" TextColor="Black" Grid.Column="0" 
                                                       Grid.Row="0" Grid.ColumnSpan="2" HorizontalOptions="Center">
                                                </Label> 
                                                 <controls:CustomRadioButton HeightRequest="15" HorizontalOptions="End" Checked="{Binding Radiobtn}" IsVisible="true"  Grid.Row="0" Grid.Column="3"/>
                                            </Grid>
                                    </ViewCell>
                                </DataTemplate>  
                            </ListView.ItemTemplate>  
                        </ListView>
                </StackLayout>

Solution

  • You haven't posted the code for objItemSelected, so you may need to mix it with my answer, but to achieve what you want you should have this:

    public object objItemSelected
    {
      get => null;
      set
      {
         if (value == null)
            return;
         value.Radiobtn = true;
         onPropertyChanged(nameof(objItemSelected));
      }
    }