Home > Back-end >  C# Getting SwipeView Item/Object that invoked event
C# Getting SwipeView Item/Object that invoked event

Time:05-26

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

CodePudding user response:

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
}
  • Related