2014-04-08 14 views
0

我想創建上下文菜單項集合,每個項目都有頭部,命令,可以執行命令,還可以添加一個新的功能,如'canExecute '但有其他條件。DelegateCommand <T>與另一個CanSee func <T,bool>,除了CanExecute

當我按下一排我DataGrid我想創建具有一定到上下文菜單項源(ItemContainerStyle)收集上下文菜單項新的上下文菜單。 我想在每個菜單項執行2個功能:

  1. CanExecute - 用於禁用/啓用項
  2. CanSee - 改變上下文菜單項的情況下,知名度,這是不相關的項目。

這樣做的最佳方法是什麼?

回答

1

您必須實施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); 
    } 
} 
+0

這幾乎與我所做的一樣。我的問題是如何在xaml中使用它 – user436862

+1

您可以更詳細地描述語句「在XAML中如何使用」?我們只在UI中綁定命令。 –

相關問題