Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading