Forum Xamarin.Forms

Can I disable ViewCell.ContextActions based on a condition

AndrewStephens.1111AndrewStephens.1111 USUniversity ✭✭
edited June 2016 in Xamarin.Forms

Hi I using a Xamarin Forms ListView and I want to know if I can disable the Context Actions based on a certain binding or in the code behind
I am using one GroupedListView for the whole application but it displays different data based on what the user is doing. There is a "Manage your Favorites" feature where I want the user to be able to swipe-to-delete on iOS or long-press on android to remove a ListItem, but I do not want this behavior if the list is displaying some search result or something else

<ViewCell.ContextActions>
    <MenuItem Text="Delete" IsDestructive="true" CommandParameter="{Binding .}" Command="{Binding Path=BindingContext.OnDeleteCommand, Source={x:Reference Name=ListViewPage}}"/>
</ViewCell.ContextActions>

This did not disable it...

<ViewCell.ContextActions IsEnabled="false"> //This IsEnabled does nothing
    <MenuItem Text="Delete" IsDestructive="true" CommandParameter="{Binding .}" Command="{Binding Path=BindingContext.OnDeleteCommand, Source={x:Reference Name=ListViewPage}}"/>
</ViewCell.ContextActions>

Best Answer

Answers

  • ajay.bhandiwadajay.bhandiwad USUniversity

    Hi @AndrewStephens.1111 It worked like a charm! Thank you :smile:

  • AnatoliiBAnatoliiB USUniversity ✭✭

    @AndrewStephens.1111 do you know how to turn off the right-arrow on the ViewCell? My ViewCell doesn't have any ContextActions

  • Hikari91Hikari91 ITMember ✭✭✭

    @AndrewStephens.1111 said:
    For what I wanted to achieve I did the following...

    In the XAML

    <ViewCell BindingContextChanged="OnBindingContextChanged">
    

    In the code behind

    private void OnBindingContextChanged(object sender, EventArgs e)
    {
        base.OnBindingContextChanged();
    
        if (BindingContext == null)
            return;
    
        ViewCell theViewCell = ((ViewCell)sender);
        var item = theViewCell.BindingContext as ListItemModel;
        theViewCell.ContextActions.Clear();
    
        if (item != null)
        {
            if (item.ListItemType == ListItemTypeEnum.FavoritePlaces
               || item.ListItemType == ListItemTypeEnum.FavoritePeople)
            {
                theViewCell.ContextActions.Add(new MenuItem()
                {
                    Text = "Delete"
                });
            }
        }
    }
    

    Based which type of list item we are dealing with, we get to decide where to place the context actions

    It worked like a charm!
    Thank you!

    Ps. Your avatar is so funny!! xD

  • CarLoOSXCarLoOSX USMember ✭✭

    I tried to add a behavior or binding a command but with no success, I did it in code behind like this :smile:


        private void SourceChangedCommand(object sender, EventArgs e)
        {
            base.OnBindingContextChanged();
    
            if (BindingContext == null)
                return;
    
            var vm = (WaitingListViewModel)BindingContext;
    
            vm.SourceChangedCommand.Execute(sender);
        }
    
  • jtthjtth Member ✭✭

    @AndrewStephens.1111 said:
    For what I wanted to achieve I did the following...

    In the XAML

    <ViewCell BindingContextChanged="OnBindingContextChanged">
    

    In the code behind

    private void OnBindingContextChanged(object sender, EventArgs e)
    {
        . . .
    }
    

    This works beautifully BUT we are seeing a slight hiccup. Our case is one universal listView that conditionally wants a context action depending on it's data. This is achieved, thanks to you, but there is a UI "bug".

    We add context actions in our template by default and remove them if a specific entry has no data. For example - we initialize our listView with a few empty entries to best blend the UI from no entry to having entries. What this means is our first entries in the listView have their contextActions removed - great! BUT when we add entries with data the first time the listView entries are stretched out of their layouts. After "refreshing" the view this bug is fixed.

    What to do about this?

Sign In or Register to comment.