Как скрыть отключенный MenuItem в ListView - PullRequest
0 голосов
/ 09 июля 2020

Я хотел бы спросить вас, как скрыть MenuItem из ListView-BaseControls. Я не могу скрыть свою команду, когда она отключена. В MenuItem нет атрибута isVisible в качестве StackLayout, а есть только isDisable.

Я попытался включить MenuItem в блок StackLayout, где я могу настроить видимость, но не могу создать StackLayout в «List»

Как вы видите код ниже, я хочу скрыть вторую кнопку / MenuItem с параметром

isEnabled = ".... IsCommandDissabled" . Кнопка видна в каждой строке списка, но ее функциональность отключена. Теперь я бы хотел полностью скрыть эту кнопку. У вас есть какой-нибудь совет?

Я даже попытался заглянуть в файл .cs за этим xaml и поиграть в методе 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}"/>
                                    .
                                    .
                                    .
                                    .
                                    .

1 Ответ

0 голосов
/ 09 июля 2020

Прежде всего, если вы хотите добиться такого результата.

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.

     
                     

Затем вы должны создать 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;
            });
            
            
            
        }

Вот моя демонстрация, вы можете обратиться к ней. https://github.com/851265601/Xamarin.Android_ListviewSelect/blob/master/XFormsLabel.zip

...