Articles in this section
Category / Section

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

9 mins read

In .NET MAUI ListView (SfListView), you can remove an item by using a button placed inside the ItemTemplate of the SfListView.

XAML

Bind a ViewModel command to the Button’s Command property within the ListView.

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

C#

In the ViewModel, implement a command handler to remove 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

Download the complete sample from GitHub.

Conclusion

I hope you enjoyed learning how to remove a ListView item using a button in the ItemTemplate 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 with configuration specifications. Explore our .NET MAUI ListView example to understand how to create and manipulate data.

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

Please let us know in the comments section below if you have any queries or require clarification. 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