Category / Section
How to remove ListView item using itemtemplate button in Xamarin.Forms (SfListView)
You can delete an item from ListView by the button loaded inside ItemTemplate in Xamarin.Forms.
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");
}
}
}
Output
