如何使用 Xamarin Forms 在列表视图单元格的选定项目上使用带有单选按钮的已检查绑定?

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

我在列表视图中使用单选按钮,在列表视图项目上单击我想制作单选按钮 checked/selected 但不幸的是无法 select 单选按钮。

                       <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>

根据你的描述,我猜你想要有两个事件,一个是ListView Itemselected,另一个是Listview中的RadioButton检查事件。我做了一个关于这个的例子,但是我在listview中添加了Button,替换了RadioButton,你可以看看:

 <StackLayout>
    <ListView ItemsSource="{Binding models}" RowHeight="40" ItemSelected="ListView_ItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">

                            <Label
                                x:Name="label1"
                                Text="{Binding Name}"
                                TextColor="Black" />
                            <Button x:Name="btn1" Text="{Binding Description}" Clicked="OnButtonClick" />


                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

  private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        testmodel model = (testmodel)e.SelectedItem;
        //Console.WriteLine(model.Name);
        DisplayAlert("Alert", model.Name, "OK");
    }

    private void OnButtonClick(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        StackLayout listviewiten = (StackLayout)btn.Parent;
        Label label = (Label)listviewiten.Children[0];
        DisplayAlert("Alert", label.Text, "OK");
        //Console.WriteLine(label.Text);


    }

您尚未发布 objItemSelected 的代码,因此您可能需要将其与我的答案混合使用,但要实现您想要的效果,您应该拥有:

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