【问题标题】:DelegateCommand<T> with another CanSee func<T,bool> except CanExecuteDelegateCommand<T> 与另一个 CanSee func<T,bool> 除了 CanExecute
【发布时间】:2014-04-08 13:34:56
【问题描述】:

我想创建上下文菜单项集合,其中每个项都有标题、命令、可以执行命令,还想添加一个新功能以提高可见性,该功能也类似于“canExecute”,但具有其他条件。

当我按下 DataGrid 中的一行时,我想创建一个新的上下文菜单,其中包含绑定到上下文菜单项源 (ItemContainerStyle) 的集合上下文菜单项。 我想在每个菜单项上执行 2 个功能:

  1. CanExecute - 用于禁用/启用项目
  2. CanSee - 用于更改上下文菜单项的可见性,以防它与项目无关。

最好的方法是什么?

【问题讨论】:

    标签: c# wpf contextmenu icommand delegatecommand


    【解决方案1】:

    你必须已经实现了DelegateCommand&lt;T&gt;,所以,在构造函数中传递另一个Func&lt;T,bool&gt;,并从CanExecute()方法按位返回canExecute委托和canSee委托的(&&)。

    public class DelegateCommand<T> : ICommand
    {
       private readonly Action<T> executeMethod;
       private readonly Func<T, bool> canExecuteMethod;
       private readonly Func<T, bool> canSeeMethod;
    
       public DelegateCommand(Action<T> executeMethod) : this(executeMethod, null)
       {}
    
       public DelegateCommand(Action<T> executeMethod,
                              Func<T, bool> canExecuteMethod)
                : this(executeMethod, canExecuteMethod, null)
       {}
    
       public DelegateCommand(Action<T> executeMethod,
                              Func<T, bool> canExecuteMethod,
                              Func<T, bool> canSeeMethod)
       {
          this.executeMethod = executeMethod;
          this.canExecuteMethod = canExecuteMethod;
          this.canSeeMethod = canSeeMethod;
       }
    
       ...... //Other implementations here
    
       public bool CanExecute(T parameter)
       {
          if (canExecuteMethod == null) return true;
          return canExecuteMethod(parameter) && canSeeMethod(parameter);
       }
    }
    

    【讨论】:

    • 这和我做的差不多。我的问题是如何在 xaml 中使用它
    • 声明-How to use in XAML能再详细一点吗?我们只绑定 UI 中的命令。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多