Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

c# - 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? 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:

<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>
question from:https://stackoverflow.com/questions/65842623/how-to-do-delete-button-that-delete-one-cell-in-collection-view-and-row-in-sqlit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...