C# - Xamarin表单删除了切换列表视图项

时间:2017-11-22 16:30:50

标签: c# android listview xamarin.forms

所以,我想删除我切换的listview中的项目。我有这样的列表视图:

Result

现在我希望当我点击要选择的内容时,它将从列表视图中删除,但是如果用户确定该项是否可以删除,则用户必须首先提示弹出状态

这是我现在的代码:

private async void Switch_Toggled(object sender, ToggledEventArgs e)
        {
            Tasks task = (sender as Switch).BindingContext as Tasks;
            await ApiManager.UpdateTasksFromListAsync(task);
        }

这只是更新API的值。但是如何从列表视图中删除/隐藏它?

这是xaml代码:

<ListView x:Name="lvwDetailPage" SeparatorVisibility="None">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="70" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                        </Grid.RowDefinitions>

                        <Switch IsToggled="{Binding completed}" Grid.Column="0" Toggled="Switch_Toggled" />
                        <Label Grid.Column="1" Text="{Binding title}" TextColor="#212121" VerticalOptions="Center" Margin="10,0" />

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

1 个答案:

答案 0 :(得分:4)

    ObservableCollection<Tasks> TaskList;

    private async Task GetData(long id)
    {
        List<Tasks> tasks = await ApiManager.GetTasksFromListAsync(id);

        TaskList = new ObservableCollection<Tasks>(tasks);

        lvwDetailPage.ItemsSource = TaskList;

        lvwDetailPage.ItemSelected += LvwDetailPage_ItemSelected;
    }

    private async void Switch_Toggled(object sender, ToggledEventArgs e)
    {
        Tasks task = (sender as Switch).BindingContext as Tasks;

        if (task != null)
        {
            await ApiManager.UpdateTasksFromListAsync(task);
            TaskList.Remove(task);
        }          
    }