We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add context menu support for items in SfListView?

Platform: Xamarin.Forms |
Control: SfListView
Tags: contextmenu

The SfListView allows displaying a pop-up menu with different menu items to an item when it is long pressed by customizing the SfListView and by using custom view in it. For UWP platform, you can also display the pop-up menu when pressing right click button over the item. Display the pop-up menu by accessed the touch position in the item based on Position property from ItemHolding event.

The following code example illustrates that SfListView to display a context menu, adding different menu items and layout the pop-up menu in it.

Defining the SfListView

<ContentPage xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms">
        <local:ContactsViewModel x:Name="viewModel"/>
            <Grid >
            <syncfusion:SfListView x:Name="listView" ItemsSource="{Binding contactsinfo}">
                                    <Grid VerticalOptions="Center">
                                        <Image Source="{Binding ContactImage}" Grid.Column="0"/>
                                        <StackLayout Grid.Column="1">
                                            <Label Text="{Binding ContactName}"/>
                                            <Label Text="{Binding ContactNumber}"/>
                                    <StackLayout HeightRequest="1" BackgroundColor="Gray"/>


Defining SfPopUpView with sort and delete options

public class Behavior : Behavior<SfListView>
        SfListView ListView;
        int sortorder = 0;
        Contacts item;
        SfPopupLayout popupLayout;
        protected override void OnAttachedTo(SfListView listView)
            ListView = listView;
            ListView.ItemHolding += ListView_ItemHolding;
            ListView.ScrollStateChanged += ListView_ScrollStateChanged;
            ListView.ItemTapped += ListView_ItemTapped;
        private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
        private void ListView_ScrollStateChanged(object sender, ScrollStateChangedEventArgs e)
        private void ListView_ItemHolding(object sender, ItemHoldingEventArgs e)
            item = e.ItemData as Contacts;
            popupLayout = new SfPopupLayout();
            popupLayout.PopupView.HeightRequest = 100;
            popupLayout.PopupView.WidthRequest = 100;
            popupLayout.PopupView.ContentTemplate = new DataTemplate(() =>
                var mainStack = new StackLayout();
                mainStack.BackgroundColor = Color.Teal;
                var deletedButton = new Button()
                    Text = "Delete",
                    TextColor = Color.White
                deletedButton.Clicked += DeletedButton_Clicked;
                var sortButton = new Button()
                    Text = "Sort",
                    HeightRequest = 50,
                    BackgroundColor = Color.Teal,
                sortButton.Clicked += SortButton_Clicked;
                return mainStack;
            popupLayout.PopupView.ShowHeader = false;
            popupLayout.PopupView.ShowFooter = false;
            if (e.Position.Y + 100 <= ListView.Height && e.Position.X + 100 > ListView.Width)
                popupLayout.Show((double)(e.Position.X - 100), (double)(e.Position.Y));
            else if (e.Position.Y + 100 > ListView.Height && e.Position.X + 100 < ListView.Width)
                popupLayout.Show((double)e.Position.X, (double)(e.Position.Y - 100));
            else if (e.Position.Y + 100 > ListView.Height && e.Position.X + 100 > ListView.Width)
                popupLayout.Show((double)(e.Position.X - 100), (double)(e.Position.Y - 100));
                popupLayout.Show((double)e.Position.X, (double)(e.Position.Y));
        private void SortButton_Clicked(object sender, EventArgs e)
            if (ListView == null)
            ListView.DataSource.LiveDataUpdateMode = LiveDataUpdateMode.AllowDataShaping;
            if (sortorder == 0)
                ListView.DataSource.SortDescriptors.Add(new SortDescriptor { PropertyName = "ContactName", Direction = ListSortDirection.Descending });
                sortorder = 1;
                ListView.DataSource.SortDescriptors.Add(new SortDescriptor { PropertyName = "ContactName", Direction = ListSortDirection.Ascending });
                sortorder = 0;
        private void Dismiss()
            popupLayout.IsVisible = false;
        private void DeletedButton_Clicked(object sender, EventArgs e)
            if (ListView == null)
            var source = ListView.ItemsSource as IList;
            if (source != null && source.Contains(item))
                App.Current.MainPage.DisplayAlert("Alert", "Unable to delete the item", "OK");
            item = null;
            source = null;


Now run the application with the following output.


Download sample from GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
You must log in to leave a comment
Matthew Bailey
Feb 08, 2022

How would you stop 2 context menus from displaying at the same time?


Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile