如何在列表视图中实施xamarin表单复选框以仅删除选中的行

时间:2018-10-04 05:58:27

标签: xamarin checkbox cross-platform

我有一个列表视图,可从Web api检索数据。我已经为每行添加了复选框。现在我只想删除选中的行。怎么办?

我的Xaml文件:

        <StackLayout VerticalOptions="CenterAndExpand">
            <ListView x:Name="postsListView" ItemSelected="postsListView_ItemSelected" HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="3*"/>
                                    <ColumnDefinition Width="3*"/>
                                    <ColumnDefinition Width="3*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <!--Data From Web Api-->
                                <controls:CheckBox x:Name="checkbox2" x:Uid="{Binding Id}"  Checked="False" Grid.Row="0" Grid.Column="0"/>
                                <Label Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>
                                <Label Grid.Row="0" Grid.Column="2" Text="{Binding Address}"/>
                                <Label Grid.Row="0" Grid.Column="3" Text="{Binding Telephone}"/>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>

我无法实现侦听器和删除操作。我只需要删除选中的行。 MyXaml.cs文件:

    private String Url = "http://192.168.8.115/NewAPI/api/Employee_Table";
    private HttpClient client = new HttpClient();
    private ObservableCollection<Employee> _employee;
    Employee _Employee = new Employee();


    public AddRecordPage ()
    {
        InitializeComponent ();            
    }

    protected override async void OnAppearing()
    {
        var response = await client.GetStringAsync(Url);
        var Emp = JsonConvert.DeserializeObject<List<Employee>>(response);
        _employee = new ObservableCollection<Employee>(Emp);
        postsListView.ItemsSource = _employee;
        base.OnAppearing();
    }

1 个答案:

答案 0 :(得分:0)

您可以在Employee模型中拥有一个名为IsChecked的布尔属性,并且可以将所有雇员初始设置为false并将其绑定到Checkbox控件。轻敲列表项时,可以进行编码,以使选定项的IsChecked属性为true,反之亦然。我相信您必须有一个按钮才能删除选中的项目,然后单击按钮,检查集合中IsChecked的true员工并将其删除。让我知道是否有疑问。

相关问题