0

各項目にヘッダー、コマンドがあり、コマンドを実行できるコンテキストメニュー項目のコレクションを作成し、「canExecute」のような機能であるが他の条件を持つ可視性のための新しい機能を追加したいと考えています。

行を押しているときDataGridに、コンテキスト メニュー項目 source( ) にバインドされたコレクション コンテキスト メニュー項目を持つ新しいコンテキスト メニューを作成したいと考えていますItemContainerStyle。各メニュー項目で 2 つの機能を実行したい:

  1. CanExecute- アイテムの無効化/有効化
  2. CanSee- コンテキスト メニュー項目がアイテムに関連していない場合に、コンテキスト メニュー項目の可視性を変更するため。

それを行う最良の方法は何ですか?

4

1 に答える 1

1

そのように実装している必要があります。コンストラクターでDelegateCommand<T>別のものを渡し、メソッドからビット単位でデリゲートとデリゲートの (&&) を返します。Func<T,bool>CanExecute()canExecutecanSee

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);
   }
}
于 2014-04-08T17:04:00.093 に答える