Skip to content

This repository contains sample about how to delete an item when tapping delete button inside listview ItemTemplate in MVVM?

Notifications You must be signed in to change notification settings

SyncfusionExamples/How-to-delete-an-item-when-tapping-delete-button-inside-listview-ItemTemplate-in-MVVM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

How-to-delete-an-item-when-tapping-delete-button-inside-listview-ItemTemplate-in-MVVM

You can delete an item from ListView by the button loaded inside ItemTemplate in Xamarin.Forms.

You can also refer the following article.

https://www.syncfusion.com/kb/10173/how-to-remove-listview-item-using-itemtemplate-button-in-xamarin-forms-sflistview

XAML

ViewModel command bound to the Button Command by the reference of ListView

<ContentPage xmlns:listView="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms">
    <ContentPage.Content>
        <Grid>
           <Syncfusion :SfListView x:Name="listView" 
                           ItemsSource="{Binding contactsInfo}" >
                        < Syncfusion :SfListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                        <Grid>
                                             <Button Text="Delete" Command="{Binding Path=BindingContext.DeleteCommand, Source={x:Reference listView}}" CommandParameter="{Binding .}"/>
                                        </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ Syncfusion: SfListView.ItemTemplate>
           </ Syncfusion: SfListView>
        </Grid>
    </ContentPage.Content>
</ContentPage>

C#

In VeiwModel command handler, removed the selected item from the collection.

namespace ListViewSample 
{
    public class ContactsViewModel : INotifyPropertyChanged
    {
           public Command<object> DeleteCommand { get; set; }
           public ContactsViewModel()
       {
            DeleteCommand = new Command<object>(OnTapped);
           }
       private void OnTapped(object obj)
       {
            var contact = obj as Contacts;
            contactsinfo.Remove(contact);
            App.Current.MainPage.DisplayAlert("Message","Item Deleted :" +contact.ContactName,"Ok");
       }
       }
}

About

This repository contains sample about how to delete an item when tapping delete button inside listview ItemTemplate in MVVM?

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages