私はこの質問に対するいくつかの答えを見つけましたが、どういうわけかそれを理解できません。失礼します。
MVVMパターンに従ったWPFアプリケーションがあります。これには、ビューモデルのコマンドにバインドされているボタンが含まれています。
<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;
}
}