25

Icommand2 つのメソッド1 つのイベントが含まれています。

2 つのメソッドが何をするかは明らかですが、で提供されているイベントが何をするのか理解できませんICommand

CanExecuteChangedイベントはいつ発生しますか?

以下の説明はMSDNありますが、理解できません。

CanExecuteChangedは、コマンド操作を一元化するコマンド マネージャーが、発生したもののコマンド バインディングによってまだ実行されていないコマンドを無効にする可能性があるコマンド ソースの変更を検出した場合に発生します。

これを簡単な言葉で説明していただけますか?

ありがとう......

4

2 に答える 2

25

This event is raised by the command to notify it's consumers (i.e. Button, MenuItem) that it's CanExecute property may have changed. So if focus is moved from one TextBox to another, your command may need to be enabled/disabled. This information also needs to be passed to any controls using your command.

In general, this event simply reexposes the CommandManager.RequerySuggested event. From the RoutedCommand class:

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

The RequerySuggested event is fired quite often, as focus is moved, text selection is changed. This can also be manually raised by calling InvalidateRequerySuggested.

于 2011-06-21T13:51:43.160 に答える
8

CanExecuteChangedCanExecuteのメソッドICommandが変更されたときに発生します

一部のサードパーティ ライブラリでは、パラメーターCanExecuteChangedがイベントを発生させると、イベントも発生します。たとえば、パラメータがイベントを発生させる場合、MVVM Light Toolkit ではイベントが発生しますが、Prismでは発生しません。CanExecutePropertyChangedRelayCommandCanExecuteChangedCanExecutePropertyChangedDelegateCommand

于 2011-06-21T13:51:45.680 に答える