简体   繁体   English

DelegateCommand <T> 与另一个CanSee函数 <T,bool> 除了CanExecute

[英]DelegateCommand<T> with another CanSee func<T,bool> except CanExecute

I want to create context menu item collection that each one of the item have header, command, can execute command and also to add a new feature for visibility that is also function like 'canExecute' but with other condition. 我想创建上下文菜单项集合,每个项都有标题,命令,可以执行命令,并添加可见性的新功能,该功能也像“ canExecute”一样,但具有其他条件。

When i'm pressing on a row in my DataGrid I want to create a new context menu that have collection context menu item that bounded to the context menu items source( ItemContainerStyle ). 当我在DataGrid按一行时,我想创建一个新的上下文菜单,该菜单具有与上下文菜单项source( ItemContainerStyle )绑定的集合上下文菜单项。 I want to execute 2 function on each menu item: 我想在每个菜单项上执行2个功能:

  1. CanExecute - for disable/enable to item CanExecute用于禁用/启用项目
  2. CanSee - for changing the visibility of the context menu item in case that it is not relevant to item. CanSee用于在上下文菜单项与项目无关的情况下更改其可见性。

What is the best way to do it? 最好的方法是什么?

You must have implemented DelegateCommand<T> so, pass another Func<T,bool> in constructor and from CanExecute() method return bitwise and (&&) of canExecute delegate and canSee delegate. 您必须已实现DelegateCommand<T>因此,在构造函数中传递另一个Func<T,bool> ,然后从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);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM