简体   繁体   English

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

[英]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. 我正在将单选按钮用于listview,并在listview项上单击,我想使单选按钮处于选中/选中状态,但不幸的是无法选择单选按钮。

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

According to your description, I guess that you want to have two event, one is ListView Itemselected, another is RadioButton check event in Listview. 根据您的描述,我想您想拥有两个事件,一个是ListView Itemselected,另一个是Listview中的RadioButton check事件。 I do one sample about this, but I add Button in listview, replace RadioButton, you can take a look: 我对此进行了一个示例,但是我在列表视图中添加了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);


    }

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: 您尚未发布objItemSelected的代码,因此您可能需要将其与我的答案混合使用,但是要实现所需的功能,您应该具有以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM