)
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. (Last updated on: June 24, 2019).
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 |
Published Date: September 8, 2017 |
Last Revised Date: April 30, 2019
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">
    <ContentPage.BindingContext>
        <local:ContactsViewModel x:Name="viewModel"/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
            <Grid >
            <syncfusion:SfListView x:Name="listView" ItemsSource="{Binding contactsinfo}">
                <syncfusion:SfListView.Behaviors>
                    <local:Behavior/>
                </syncfusion:SfListView.Behaviors>
                <syncfusion:SfListView.ItemTemplate>
                        <DataTemplate>
                             <StackLayout>
                                    <Grid VerticalOptions="Center">
                                        <Image Source="{Binding ContactImage}" Grid.Column="0"/>
                                        <StackLayout Grid.Column="1">
                                            <Label Text="{Binding ContactName}"/>
                                            <Label Text="{Binding ContactNumber}"/>
                                        </StackLayout>
                                    </Grid>
                                    <StackLayout HeightRequest="1" BackgroundColor="Gray"/>
                                </StackLayout>
                          </DataTemplate>
                    </syncfusion:SfListView.ItemTemplate>
                </syncfusion:SfListView>
            </Grid>
    </ContentPage.Content>
</ContentPage>
 
 

 

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;
            base.OnAttachedTo(listView);
        }
 
        private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
        {
            popupLayout.Dismiss();
        }
 
        private void ListView_ScrollStateChanged(object sender, ScrollStateChangedEventArgs e)
        {
            popupLayout.Dismiss();
        }
 
        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",
                    HeightRequest=50,
                    BackgroundColor=Color.Teal,
                    TextColor = Color.White
                };
                deletedButton.Clicked += DeletedButton_Clicked;
                var sortButton = new Button()
                {
                    Text = "Sort",
                    HeightRequest = 50,
                    BackgroundColor = Color.Teal,
                    TextColor=Color.White
                };
                sortButton.Clicked += SortButton_Clicked;
                mainStack.Children.Add(deletedButton);
                mainStack.Children.Add(sortButton);
                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));
            else
                popupLayout.Show((double)e.Position.X, (double)(e.Position.Y));
 
        }
 
        private void SortButton_Clicked(object sender, EventArgs e)
        {
            if (ListView == null)
                return;
 
            ListView.DataSource.SortDescriptors.Clear();
            popupLayout.Dismiss();
            ListView.DataSource.LiveDataUpdateMode = LiveDataUpdateMode.AllowDataShaping;
            if (sortorder == 0)
            {
                ListView.DataSource.SortDescriptors.Add(new SortDescriptor { PropertyName = "ContactName", Direction = ListSortDirection.Descending });
                sortorder = 1;
            }
            else
            {
                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)
                return;
 
            var source = ListView.ItemsSource as IList;
 
            if (source != null && source.Contains(item))
                source.Remove(item);
            else
                App.Current.MainPage.DisplayAlert("Alert", "Unable to delete the item", "OK");
 
            item = null;
            source = null;
        }
}
 

 

Now run the application with the following output.

C:\Users\dinesh.babuyadav\AppData\Local\Microsoft\Windows\INetCache\Content.Word\Screenshot_20170908-155255.png

Download sample from GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

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
Live Chat Icon