我可以根据条件禁用ViewCell.ContextActions [英] Can I disable ViewCell.ContextActions based on a condition

查看:79
本文介绍了我可以根据条件禁用ViewCell.ContextActions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Xamarin Forms ListView,我想知道是否可以基于特定绑定或在后面的代码中禁用上下文操作.

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.

我在整个应用程序中使用了一个GroupedListView,但是它根据用户的操作显示不同的数据.有一个管理您的收藏夹"功能,我希望用户能够在iOS上轻扫以删除或在android上长按以删除ListItem,但是如果列表显示某些搜索,我就不希望出现这种情况结果或其他东西

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>

如何禁用ContextAction?我不希望用户始终能够滑动

How can I disable the ContextActions? I dont wan't the user to always be able to swipe

推荐答案

对于我想要实现的目标,我做了以下工作...

For what I wanted to achieve I did the following...

在XAML中

<ViewCell BindingContextChanged="OnBindingContextChanged">

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

这篇关于我可以根据条件禁用ViewCell.ContextActions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆