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

时间:2019-01-10 13:37:36

标签: xaml mvvm xamarin.forms xlabs

我正在使用具有列表视图的单选按钮,并在列表视图项上单击“我要选中/选择单选按钮,但不幸的是无法选择该单选按钮。”

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

2 个答案:

答案 0 :(得分:0)

根据您的描述,我想您想拥有两个事件,一个是ListView Itemselected,另一个是Listview中的RadioButton check事件。我对此做了一个示例,但是我在列表视图中添加了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);


    }

答案 1 :(得分:0)

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

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