简体   繁体   English

如何在 xamarin 的 sqlite 中删除集合视图中的一个单元格和行的删除按钮?

[英]How to do delete button that delete one cell in collection view and row in sqlite in xamarin?

How to delete cell in collection view and row in sqlite?如何删除集合视图中的单元格和 sqlite 中的行? I need to get from collection view primary key and i dont know how.我需要从集合视图中获取主键,但我不知道如何。 I want to have delete button on every view cell.我想在每个视图单元格上都有删除按钮。 I have ended up with this:我最终得到了这个:

public void RemovePlane()
{
    var db = new SQLiteConnection(_dbPath);
    db.Delete<Airplane>();
}

xaml: xaml:

<CollectionView x:Name="myCollectionView" Grid.Row="0" SelectedItem="{Binding selectedPlane}">                
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout Margin="0">
                <Frame Padding="0" BackgroundColor="#00d2ff" Margin="0, 65, 0, 0" CornerRadius="30">
                    <StackLayout Padding="20">
                        <Label Text="{Binding Airline}" TextColor ="White" FontSize="30" HorizontalOptions="Center"/>
                        <Image Source="{Binding ThumbnailUrl}" HeightRequest="200"/>
                        <Label Text="{Binding Plane, StringFormat='Plane: {0}'}" TextColor ="White" FontSize="15"/>
                        <Label Text="{Binding Airline, StringFormat='Airline: {0}'}" TextColor ="White" FontSize="15"/>
                        <Label Text="{Binding Livery, StringFormat='Livery: {0}'}" TextColor ="White" FontSize="15"/>
                        <Label Text="{Binding Registration, StringFormat='Reg: {0}'}" TextColor ="White" FontSize="15"/>
                        <Label Text="{Binding Airport, StringFormat='Airport: {0}'}" TextColor ="White" FontSize="15"/>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <StackLayout Grid.Column="0">
                                <Label Text="{Binding Date, StringFormat='Date: {0}'}" TextColor ="White" FontSize="15"/>
                                <Label Text="{Binding Comment, StringFormat='Comment: {0}'}" TextColor ="White" FontSize="15"/>
                            </StackLayout>
                            <Button Text="Delete" CornerRadius="30" Margin="10, 0" HeightRequest="20" BackgroundColor="Red" Grid.Column="1" x:Name="deleteButton"/>
                        </Grid>
                    </StackLayout>
                </Frame>
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
<CollectionView.EmptyView>                    
    <AbsoluteLayout VerticalOptions="CenterAndExpand">
        <Label FontAttributes="Bold" FontSize="30" TextColor="White" HorizontalTextAlignment="Center" Text="No planes to display" AbsoluteLayout.LayoutBounds="0.5, 0.45, 300, 50" AbsoluteLayout.LayoutFlags="PositionProportional"/>                        
        <Button Text="Import Plane" WidthRequest="10" BackgroundColor="#00d2ff" FontSize="30" FontAttributes="Bold" TextColor="White" CornerRadius="30" VerticalOptions="Center" Padding="5" AbsoluteLayout.LayoutBounds="0.5, 0.55, 280, 70" AbsoluteLayout.LayoutFlags="PositionProportional" Clicked="Button_Clicked"/>
    </AbsoluteLayout>                    
</CollectionView.EmptyView>
</CollectionView>

assign a handler for your button为您的按钮分配一个处理程序

<Button Text="Delete" Clicked="RemovePlane" ... />

in your handler在你的处理程序中

public void RemovePlane(object sender, EventArgs args)
{
    var button = (Button)sender;
    var plane = (Airplane)button.BindingContext;

    var db = new SQLiteConnection(_dbPath);
    db.Delete<Airplane>(plane);

    // finally, either refresh your ItemsSource from your db
    // or if you are using an ObservableCollection just remove
    // it from the collection
}

I have done this and and when i refresh it works.我已经这样做了,当我刷新它时它就可以工作了。 One question it delete whole row?一个问题它删除了整行? And can it be improved somehow?它可以以某种方式改进吗?

private void deleteButton_Clicked(object sender, EventArgs e)
    {
        var button = (Button)sender;
        var plane = (Airplane)button.BindingContext;

        var db = new SQLiteConnection(_dbPath);
        db.Delete<Airplane>(plane.Id);
    }

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

相关问题 Xamarin/SQLite/C# - 如何使用菜单项删除从列表视图和 SQLite 表中删除一行 - Xamarin/SQLite/C# - How to delete a row from listview and SQLite table with menu item delete 如何删除一行并更新其余的某个单元格? - How to delete one row and update a certain cell in the rest? 如何使用按钮软删除 DataGrid 视图中的选定行 - How to soft delete a selected row in a DataGrid View using a button Xamarin Forms - 从 ListView 和 Observable 集合中删除行 - Xamarin Forms - Delete Row from ListView and Observable Collection Xamarin iOS UITableView,如何删除第一行? - Xamarin iOS UITableView, How to delete first row? 如何在datagrid视图中删除行并在Delete按钮单击事件中更新MySQL数据库? - How can I delete a row in a datagrid view and update the MySQL database on a delete button click event? 如何在WPF中为DataGrid提供浮动删除行按钮? - How do I have a floating delete row button for a DataGrid in WPF? 如何以编程方式删除自定义集合中的行 - How to programmatically delete a row in custom collection 如何在GridView中删除特定行的行或禁用删除按钮? - How to Delete a Row or Disable Delete Button for specific row in GridView? 用于删除 Xamarin.Forms 中的当前 ListView 行的按钮 - Button to delete current ListView Row in Xamarin.Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM