C# Getting SwipeView Item/Object that invoked event

Sorry, new here. I’ve got a CollectionView of objects and there fields, when I invoke the delete event using SwipeView I want to be able to use the object that was selected to pass into an SQLite query to delete that object from the database. My original plan was to use the Id of the object to delete it but I’m having trouble doing that. Rather than getting the properties of the object can I get the object itself to use?

XAML

                    <SwipeView>
                        <SwipeView.RightItems>
                            <SwipeItems SwipeBehaviorOnInvoked="RemainOpen">
                                <SwipeItem Text="Delete" BackgroundColor="DarkGray" 
                                           Invoked="DeleteItem_Invoked"
                                           BindingContext="{Binding ID}">
                                </SwipeItem>
                            </SwipeItems>
                        </SwipeView.RightItems>

SQL

    public Task<int> DeleteBird(Bird bird)
    {
        return _database.DeleteAsync(bird);
    }

C#

    private void DeleteItem_Invoked(object sender, EventArgs e)
    {
        
        var swipeview = sender as SwipeItem;
        var id = swipeview.BindingContext;
        Database.DeleteBird(id); 
        Debug.WriteLine(id);
    }

Github

>Solution :

use the CommandParameter (do not set BindingContext)

<SwipeItem Invoked="DeleteItem_Invoked" CommandParameter="{Binding .}" />

then in the event handler

private void DeleteItem_Invoked(object sender, EventArgs e)
{
    var swipeview = sender as SwipeItem;
    var item = (MyClassName)swipeview.CommandParameter;
    
    // item should be the selected item
}

Leave a Reply