2012-11-21 75 views
0

雖然我發現這個問題的幾個答案,我不知道怎麼辦。所以請原諒我問。如何啓用/禁用按鈕?

我有一個WPF應用程序遵循MVVM模式。它包含在視圖模型綁定到的命令的按鈕:

<button Content="Login" Command="{Binding ProjectLoginCommand}"/>

的命令使用RelayCommand。現在我想做以下事情:

  • 用戶單擊按鈕並執行相應的命令。這工作。
  • 在此命令中,另一個按鈕應被禁用,即不可點擊。

我發現這應該是可能的使用CanExecute但誠實:我根本不明白它。鋤頭可以將按鈕設置爲啓用/禁用?

這是RelayCommand.cs

namespace MyApp.Helpers { 
    class RelayCommand : ICommand { 

    readonly Action<object> execute; 
    readonly Predicate<object> canExecute; 

    public RelayCommand(Action<object> execute) : this(execute, null) { 
    } 

    public RelayCommand(Action<object> execute, Predicate<object> canExecute) 
    { 
     if (execute == null) 
      throw new ArgumentNullException("execute"); 

     this.execute = execute; 
     this.canExecute = canExecute;   
    } 

    public bool CanExecute(object parameter) 
    { 
     return canExecute == null ? true : canExecute(parameter); 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

    public void Execute(object parameter) 
    { 
     execute(parameter); 
    } 
} 
} 

這是我如何調用命令:

RelayCommand getProjectListCommand; 

public ICommand GetProjectListCommand { 
    get { 
     if (getProjectListCommand == null) { 
      getProjectListCommand = new RelayCommand(param => this.ProjectLogin()); 
     } 
     return getProjectListCommand; 
    } 
} 
+1

你可以顯示兩個命令的'Execute'和'CanExecute'的代碼嗎? – Rachel

+0

添加了命令用法。但實際上,除了'RelayCommand'類中的代碼之外,我沒有'Execute'或'CanExecute'的代碼。 –

+0

謝謝羅伯特。按鈕的啓用/禁用自動綁定到'Command.CanExecute',因此您需要讓Button1在運行時將'CanButton2Execute'設置爲false,然後Button2將被禁用。 – Rachel

回答

1

當您使用RelayCommand時,您可以指定兩種方法。第一種方法是調用該命令時要運行的主要方法。你使用第二種方法添加檢查,如驗證,這應該返回一個布爾值。如果它返回false,那麼main方法將不會運行。

它如何影響你綁定命令的按鈕,它會不斷地運行布爾方法,並且當它返回false時,那麼命令綁定的按鈕將被禁用。

所以在您的命令屬性:

public ICommand GetProjectListCommand { 
get { 
    if (getProjectListCommand == null) { 
     getProjectListCommand = new RelayCommand(param => this.ProjectLogin(), CanProjectLogin()); 
    } 
    return getProjectListCommand; 
} 

添加新的方法:

public bool CanProjectLogin() 
{ 
    //here check some properties to make sure everything is set that you'd want to use in your ProjectLogin() method 
} 

你可以看到,如果你把一個破發點的布爾方法CanExecute是如何工作的。

+0

這個解釋使我理解:-)但是,第二個參數缺少param =>語句。 –

3

在創建RelayCommand對象,你必須通過一個能斷定,這可能是(其他中東西)具有以下簽名的方法:

bool MethodName(對象參數)。

如果您不需要該參數,請使用例如bool MethodName(),但將它傳遞給RelayCommand構造函數,如下所示:(o)=> MethodName()。

在此方法中,您應該執行您的邏輯並返回一個值,指示命令是否可以執行。其餘的應該由WPF Command基礎設施來處理。

+0

忘記指定:傳遞謂詞作爲構造函數中的第二個參數: new RelayCommand(param => this.ProjectLogin(),param => this.MethodName()) –

+0

謝謝,這個評論與emybobs組合的答案只是使它是正確的:-) –

1

如果您有麻煩與canExecute回調的工作,你可能會發現很容易與RelayCommand的一個簡化版本的工作:

class RelayCommand : ICommand 
{ 
    readonly Action execute; 
    private bool canExecute; 

    public RelayCommand(Action execute) 
    { 
     this.execute = execute; 
     this.canExecute = true; 
    } 

    public bool CanExecute(object parameter) 
    { 
     return canExecute; 
    } 

    public void SetCanExecute(bool canExecute) 
    { 
     this.canExecute = canExecute; 
     var handler = CanExecuteChanged; 
     if (handler != null) handler(this, EventArgs.Empty); 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 
     execute(); 
    } 
} 

通過這種方法,您保存到RelayCommand對象,您做參考,允許你禁用這樣的命令:

getProjectListCommand.SetCanExecute(false);