First of all, if you want to achieve this result.

Here is a workaround about hide a items in MenuItem
.
First of all, you need to create two DataTemplate
like following code. Note: you must add CachingStrategy="RecycleElement"
in the listview
, add it, DataTemplate will chanage dynamically in the Listview
.
<ContentPage.Resources>
<DataTemplate x:Key="OneItemTemplate">
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="{Binding Item1Text}" />
</ViewCell.ContextActions>
<StackLayout>
<Label Text="{Binding Text}" />
<Button Text="More" VerticalOptions="StartAndExpand" Command="{ Binding BindingContext.MyToggleLegacyMode, Source={x:Reference Name=BillView} }" CommandParameter="{Binding .}" />
</StackLayout>
</ViewCell>
</DataTemplate>
<DataTemplate x:Key="TwoItemsTemplate">
<ViewCell >
<ViewCell.ContextActions>
<MenuItem Text="{Binding Item1Text}" />
<MenuItem Text="{Binding Item2Text}" />
</ViewCell.ContextActions>
<StackLayout>
<Label Text="{Binding Text}" />
<Button Text="More" VerticalOptions="StartAndExpand" Command="{ Binding BindingContext.MyToggleLegacyMode, Source={x:Reference Name=BillView} }" CommandParameter="{Binding .}" />
</StackLayout>
</ViewCell>
</DataTemplate>
<local:ItemDataTemplateSelector x:Key="ItemDataTemplateSelector"
OneItemTemplate="{StaticResource OneItemTemplate}"
TwoItemsTemplate="{StaticResource TwoItemsTemplate}" />
</ContentPage.Resources>
<StackLayout>
<ListView x:Name="BillView" ItemsSource="{Binding Items}" HasUnevenRows="True" CachingStrategy="RecycleElement"
ItemTemplate="{DynamicResource ItemDataTemplateSelector}" />
</StackLayout>
Then you should create a DataTemplateSelector
to switch this two DataTemplate
.
public class ItemDataTemplateSelector : DataTemplateSelector
{
public DataTemplate OneItemTemplate { get; set; }
public DataTemplate TwoItemsTemplate { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
return ((ContextMenuItem)item).Type == ContextMenuItemType.OneItem ? OneItemTemplate : TwoItemsTemplate;
}
}
In the Viewmodel, it will change the DataTemplate(For testing, I just set the two item to the one item, you can change it by your needs) .
public ICommand MyToggleLegacyMode { get; set; }
public ObservableCollection<ContextMenuItem> Items { get; private set; }
public AndroidViewCellPageViewModel()
{
IsContextActionsLegacyModeEnabled = false;
Items = new ObservableCollection<ContextMenuItem>();
Items.Add(new ContextMenuItem { Text = "Cell 1", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 2", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 3", Type = ContextMenuItemType.OneItem });
Items.Add(new ContextMenuItem { Text = "Cell 4", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 5", Type = ContextMenuItemType.OneItem });
Items.Add(new ContextMenuItem { Text = "Cell 6", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 7", Type = ContextMenuItemType.OneItem });
Items.Add(new ContextMenuItem { Text = "Cell 8", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 9", Type = ContextMenuItemType.OneItem });
Items.Add(new ContextMenuItem { Text = "Cell 10", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 11", Type = ContextMenuItemType.OneItem });
Items.Add(new ContextMenuItem { Text = "Cell 12", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 13", Type = ContextMenuItemType.OneItem });
Items.Add(new ContextMenuItem { Text = "Cell 14", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 15", Type = ContextMenuItemType.OneItem });
Items.Add(new ContextMenuItem { Text = "Cell 16", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 17", Type = ContextMenuItemType.OneItem });
Items.Add(new ContextMenuItem { Text = "Cell 18", Type = ContextMenuItemType.TwoItems });
Items.Add(new ContextMenuItem { Text = "Cell 19", Type = ContextMenuItemType.OneItem });
Items.Add(new ContextMenuItem { Text = "Cell 20", Type = ContextMenuItemType.TwoItems });
MyToggleLegacyMode = new Command((key) => {
var contextMenuItem=key as ContextMenuItem;
contextMenuItem.Type = ContextMenuItemType.OneItem;
});
}
Here is my demo, you can refer to it.
https://github.com/851265601/Xamarin.Android_ListviewSelect/blob/master/XFormsLabel.zip