0

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.

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). I want to execute 2 function on each menu item:

  1. CanExecute - for disable/enable to item
  2. CanSee - for changing the visibility of the context menu item in case that it is not relevant to item.

What is the best way to do it?

dcastro
  • 66,540
  • 21
  • 145
  • 155
user436862
  • 867
  • 2
  • 15
  • 32

1 Answers1

1

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.

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);
   }
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185