WPF - How to force a Command to re-evaluate 'CanExecute' via its CommandBindings Ask Question

WPF - How to force a Command to re-evaluate 'CanExecute' via its CommandBindings Ask Question

I have a Menu where each MenuItem in the hierarchy has its Command property set to a RoutedCommand I've defined. The associated CommandBinding provides a callback for the evaluation of CanExecute which controls the enabled state of each MenuItem.

This almost works. The menu items initially come up with the correct enabled and disabled states. However when the data that my CanExecute callback uses changes, I need the command to re-request a result from my callback in order for this new state to be reflected in the UI.

There do not appear to be any public methods on RoutedCommand or CommandBinding for this.

コントロールをクリックまたは入力すると、コールバックが再度使用されることに注意してください (マウスオーバーでは更新が行われないため、入力時にトリガーされると思われます)。

ベストアンサー1

あまりきれいではありませんが、CommandManager を使用してすべてのコマンドバインディングを無効にすることができます。

CommandManager.InvalidateRequerySuggested();

詳細は以下を参照マイクロソフト

おすすめ記事