首页 > 解决方案 > 如何在 ListView 中隐藏禁用的 MenuItem

问题描述

我想问你,如何从 ListView-BaseControls 中隐藏 MenuItem。禁用时我无法隐藏我的命令。MenuItem 没有作为 StackLayout 的属性“isVisible”,而只有“isDisable”。

我试图将 MenuItem 覆盖到 StackLayout 块中,我可以在其中设置可见性,但我无法在“列表”中创建 StackLayout

正如您在下面看到的代码,我想隐藏具有参数的第二个按钮/MenuItem

isEnabled=".... IsCommandDissabled"。按钮在每个列表行上可见,但“禁用”功能。现在我想完全隐藏这个按钮。 你有什么建议吗?

我什至试图查看这个 xaml 后面的 .cs 文件并在方法 OnBindingContextChanged() 中播放...但没有成功。

<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}"/>
                                    .
                                    .
                                    .
                                    .
                                    .

标签: listviewxamarinmenuitem

解决方案


首先,如果你想达到这个结果。

在此处输入图像描述

这是关于隐藏项目的解决方法MenuItem

首先,您需要创建两个DataTemplate类似以下代码。注意:必须CachingStrategy="RecycleElement"在 中listview添加,添加它,DataTemplate 会在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>
    

然后你应该创建一个DataTemplateSelector来切换这两个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;
        }
    }

在Viewmodel中,它会改变DataTemplate(为了测试,我只是将两项设置为一项,您可以根据需要更改)。

 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;
            });
            
            
            
        }

这是我的demo,大家可以参考。 https://github.com/851265601/Xamarin.Android_ListviewSelect/blob/master/XFormsLabel.zip


推荐阅读