Using Context Actions in Xamarin.Forms ListView

One of the news in Xamarin.Forms 1.3 is what Xamarin call context actions. With context actions you can make a ListView much more richer. What you can do is to add a menu to every row in the ListView. On Windows Phone and Android is the menu showing when you're long pressing on a item, on iOS you will swipe left on the item to show the menu. As you can see on the screenshots below the menu looks different on each platform. On Windows Phone the menu will pop up, on iOS it will be be on the row and on Android at the top of the screen.

contextactions contextactions_android contextactions_ios

To use context action add them to the ViewCell in the ItemTemplate as shown in the code below.

       <MenuItem Text="Share" />
       <MenuItem Text="Buy" Command="{Binding AddToBasket}" />

MenuItem has four properties that is good to know:
Text: What's shown to the user
Clicked: Adds a event listener to the menu item.
Command: Bind a command to the menu item.
IsDestructive: Will make the menu item red on iOS if it's set to true.

 <ListView ItemsSource="{Binding Products}" VerticalOptions="Start" RowHeight="40">
              <MenuItem Text="Share" />
              <MenuItem Text="Buy" Command="{Binding AddToBasket}" />
            <ContentView Padding="0,10">
                  <ColumnDefinition Width="2*" />
                  <ColumnDefinition Width="*" />
              <Label Text="{Binding Product.Name}" TextColor="Black" />
              <Label Grid.Column="1" HorizontalOptions="End" Text="{Binding Product.Price, StringFormat='{0:0.00} kr'}" TextColor="Red" />

3 thoughts on “Using Context Actions in Xamarin.Forms ListView

  1. SanChan says:

    Hello there,

    I am having trouble accessing the Menu Items when using Rg.Plugins.Popup.Page. I can see the menu Items, but they are in the background, so I can;t click on them without closing the popup page, please advise, thanks!

    (Issue is present on @ Android project, the other two projects are just fine because the menu is accessible in the current Popup Page)


  2. Ray says:

    It’s too bad that the actual item in the ListView does not also become the selected item (highlighted) after the long press. Have you resolved this anomaly?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.