在打开上下文菜单时提高上下文菜单命令的CanExcute [英] Raise context menu command's CanExcute while open the context menu

查看:198
本文介绍了在打开上下文菜单时提高上下文菜单命令的CanExcute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行上下文菜单时可以执行ICommand打开

继续上面的查询,仍然无法实现。

With the continuation of the above query, Still its not able to achieve.

在打开上下文菜单时,是否有任何方法可以调用上下文菜单命令的CanExcute。

Is there any way to raise context menu command's CanExcute while open the context menu. This need to be done in the context menu itself and here am not able to access the viewmodel.

对此有任何想法吗?

    public static BaseCommand SaveCommand
    {
        get
        {
            if (saveCommand == null)
                saveCommand = new BaseCommand(OnSaveCommandClicked, OnSaveCommandCanExcute);

            return saveCommand;
        }
    }

其中BaseCommand派生自ICommand。

where BaseCommand is derived from ICommand.

public class BaseCommand : ICommand
{
    private Predicate<object> _canExecute;
    private Action<object> _method;
    public event EventHandler CanExecuteChanged;

    public BaseCommand(Action<object> method)
        : this(method, null)
    {
    }

    public BaseCommand(Action<object> method, Predicate<object> canExecute)
    {
        _method = method;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        if (_canExecute == null)
        {
            return true;
        }

        return _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _method.Invoke(parameter);
    }

}


推荐答案

BaseCommand 中,将 CanExecuteChanged 替换为以下内容。

Inside BaseCommand, replace the CanExecuteChanged to look like the following. And it will work the way you want it.

   public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }

这篇关于在打开上下文菜单时提高上下文菜单命令的CanExcute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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