0

I would like to ask you, how to hide MenuItem from ListView-BaseControls. I am not able to hide my command when its disabled. MenuItem have dont have attibut "isVisible" as StackLayout, but only "isDisable".

I tried to cover MenuItem into StackLayout block where I can set up Visibility but I cannot create StackLayout in "List"

As you see code bellow, I wanna to hide second button/MenuItem which have parameter

isEnabled=".... IsCommandDissabled". Button is visible on each list line but "dissabled" functionality. Now I would like to fully hide this button. Do you have any advice?

I even tried to look into .cs file behind this xaml and play in method OnBindingContextChanged()... But without success.

<StackLayout>
    <ListView
        AutomationId="objectList"
        ItemsSource="{Binding Items}">
            <x:Arguments>
                <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
            </x:Arguments>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <BaseControls:MenuItemsDisablingViewCell>
                        <BaseControls:MenuItemsDisablingViewCell.ContextActions>
                            <MenuItem
                                    IsDestructive="True"
                                    Command="{Binding EntViewModel.DeleteCom}"
                                    Text="Delete"/>
                            <MenuItem 
                                    Command="{Binding DissableAndHidenCommand}"
                                    Text="Command which is Dissabled and hiden"
                                    IsEnabled="{Binding IsCommandDissabled}"/>
                                    .
                                    .
                                    .
                                    .
                                    .
user3132499
  • 33
  • 1
  • 6

1 Answers1

0

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

enter image description here

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

Leon
  • 8,404
  • 2
  • 9
  • 52
  • @user3132499 Do you get some problem when you use above code? – Leon Jul 10 '20 at 08:13
  • Are there any update for this issue? If above answer is helpful, please accept it as answer(click the ✔ in the upper left corner of this answer), it will help others who have similar issue. – Leon Jul 14 '20 at 09:50