Articles in this section
Category / Section

How to add or remove an item from .NET MAUI ListView(SfListView)?

4 mins read

The .NET MAUI ListView (SfListView) allows you to add or remove items by updating the collection bound to the SfListView.ItemsSource from the  ViewModel.

XAML

ViewModel commands are bound to the view for adding or removing an item.

<StackLayout>
    <Grid HeightRequest="50" ColumnSpacing="5" Padding="5"> 
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button Text="Add item" Grid.Column="0" Command="{Binding AddCommand}"/>
        <Button Text="Delete last item" Grid.Column="1" Command="{Binding DeleteCommand}"/>
    </Grid>
    <listView:SfListView x:Name="listView" ItemSize="70" ItemsSource="{Binding ContactsInfo}" VerticalOptions="FillAndExpand">
        ...
    </listView:SfListView>
</StackLayout>

C#

A new object has been added to the collection of ViewModel.

public class ContactsViewModel
{
    public Command AddCommand { get; set; }
 
    public ContactsViewModel()
    {
        AddCommand = new Command(OnAddTapped);
    }
 
    private void OnAddTapped(object obj)
    {
        var details = new Contacts()
        {
            ContactName = CustomerNames[r.Next(1, 50)],
            ContactNumber = r.Next(100, 400).ToString() + "-" + r.Next(500, 800).ToString() + "-" + r.Next(1000, 2000).ToString(),
            ContactImage = "people_circle" + r.Next(0, 19) + ".png"
        };
 
        ContactsInfo.Add(details);
    }
}

C#

Object has been removed from the collection of ViewModel.

public class ContactsViewModel
{
    public Command DeleteCommand { get; set; }
 
    public ContactsViewModel()
    {
        DeleteCommand = new Command(OnDeleteTapped);
    }
 
    private void OnDeleteTapped(object obj)
    {
        if (ContactsInfo.Count > 0)
            ContactsInfo.Remove(ContactsInfo[ContactsInfo.Count - 1]);
        else
            App.Current.MainPage.DisplayAlert("Alert", "There is no item in the list", "OK");
    }
}

Download the complete sample on GitHub

Conclusion

I hope you enjoyed learning how to add or remove an item from .NET MAUI ListView.

You can refer to our .NET MAUI ListView feature tour page to learn 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.

For current customers, you can check out our components from the License and Downloads page. 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. Contact us through our support forumsDirect-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