Articles in this section
Category / Section

How to remove ListViewItem using button in item template in .NET MAUI ListView(SfListView)?

9 mins read

You can delete an item from .NET MAUI ListView (SfListView) by the button loaded inside ItemTemplate.

XAML

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

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ListViewMaui"
             xmlns:listView="clr-namespace:Syncfusion.Maui.ListView;assembly=Syncfusion.Maui.ListView"
             xmlns:dataSource="clr-namespace:Syncfusion.Maui.DataSource;assembly=Syncfusion.Maui.DataSource"
             x:Class="ListViewMaui.MainPage">

    <ContentPage.BindingContext>
        <local:ContactsViewModel />
    </ContentPage.BindingContext>

    <ContentPage.Content>
        <Grid RowSpacing="0">
            <listView:SfListView x:Name="listView" ItemSize="70" SelectionMode="Multiple" IsStickyFooter="True"
                       ItemsSource="{Binding Items}" ItemSpacing="0,0,5,0" >

                <listView:SfListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <Grid >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid x:Name="grid" RowSpacing="1" >
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="1" />
                                        </Grid.RowDefinitions>
                                        <Grid RowSpacing="1">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="70" />
                                            </Grid.ColumnDefinitions>

                                            <Grid Grid.Column="0"
                  RowSpacing="1"
                  Padding="10,0,0,0"
                  VerticalOptions="Center">
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="*" />
                                                    <RowDefinition Height="*" />
                                                </Grid.RowDefinitions>

                                                <Label LineBreakMode="NoWrap"
                     TextColor="#474747"
                     Text="{Binding ContactName}">
                                                </Label>
                                                <Label Grid.Row="1"
                     Grid.Column="0"
                     TextColor="#474747"
                     LineBreakMode="NoWrap"
                     Text="{Binding ContactNumber}">
                                                </Label>
                                            </Grid>
                                        </Grid>
                                        <StackLayout Grid.Row="1" BackgroundColor="Gray" HeightRequest="1"/>
                                    </Grid>
                                    <Button WidthRequest="150" Text="Delete" Grid.Column="1" Command="{Binding Path=BindingContext.DeleteCommand, Source={x:Reference listView}}" CommandParameter="{Binding .}"/>
                                </Grid>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </listView:SfListView.ItemTemplate>

            </listView:SfListView>
        </Grid>
    </ContentPage.Content>

</ContentPage>

C#

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

using System;
using System.Collections.Generic;
namespace ListViewMaui
{
    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;
            Items.Remove(contact);
            App.Current.MainPage.DisplayAlert("Message", "Item Deleted :" + contact.ContactName, "Ok");
        }
    }
}

Output:

RemoveListViewItem.gif

View Sample in GitHub

Conclusion

I hope you enjoyed learning how to remove ListView item using itemtemplate button in .NET MAUI ListView.

You can refer to our .NET MAUI ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET MAUI ListView example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied