0

我正在使用带有列表视图的单选按钮,并在列表视图项目上单击我想选中/选中单选按钮,但不幸的是无法选择单选按钮。

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

2 回答 2

0

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));
  }
}
于 2019-01-11T10:35:40.733 回答
0

根据你的描述,我猜你想要两个事件,一个是 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);


    }
于 2019-01-11T07:39:52.197 回答