I use Xamarin.Forms to define a ListView
. This ListView
defines some ContextAction
s on inside the ViewCell
. Depending on the platform, these context actions are then presented to the user. In Android, this is triggered by long-pressing the specific item. Sadly, this Item will not be (properly) highlighted, as can be seen in this screenshot (I long-pressed Third Item, sadly I can't yet embed images).
Is there a way to modify the Cell when the context menu opens? Specifically asking for a solution for Android, but a general answer is welcome as well. The goal eventually is to improve highlighting, e.g. by changing the cell's background color. Modifying the cell, when one ContextAction
is pressed, is not what I am looking for.
I browsed through the source code of Xamarin.Forms and thought about somehow inheriting from e.g. the ViewCell class, but couldn't find an event or command that would be triggered / called upon long-pressing an item. I have set up a simple repository to which illustrates the behavior: GitHub repository
The most important code snippets
ListView definition in XAML
<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ListViewContextMenu" x:Class="ListViewContextMenu.ListViewContextMenuPage"> <ListView x:Name="MyListView"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.ContextActions> <MenuItem Text="Action" Command="{Binding OnAction}" CommandParameter="{Binding .}"/> </ViewCell.ContextActions> <Label Text="{Binding Name}" /> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </ContentPage>
MyItem definition (MVVM)
using System.Diagnostics; using Xamarin.Forms; namespace ListViewContextMenu { public class MyItem { public string Name { get; set; } public Command OnAction { get; set; } public MyItem() { OnAction = new Command((obj) => Debug.WriteLine($"Item {obj.ToString()} clicked")); } } }