WPF - 如何强制命令通过其CommandBindings重新评估“CanExecute” [英] WPF - How to force a Command to re-evaluate 'CanExecute' via its CommandBindings

查看:360
本文介绍了WPF - 如何强制命令通过其CommandBindings重新评估“CanExecute”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个菜单其中每个 MenuItem 在其层次中有 code>属性设置为 RoutedCommand 我已经定义。相关的 CommandBinding CanExecute 的评估提供回调,控制每个 MenuItem



这几乎工作。菜单项最初产生正确的启用和禁用状态。但是当我的 CanExecute 回调使用的数据更改,我需要重新请求一个结果从我的回调的命令为了这个新的状态反映在UI。



RoutedCommand CommandBinding $ p

解决方案

不是本书中最漂亮的,但可以使用CommandManager来使所有命令绑定无效:

  CommandManager.InvalidateRequerySuggested(); 

查看更多信息 MSDN


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.

Note that the callback is used again when I click or type into the control (I guess it's triggered on input because mouse-over doesn't cause the refresh).

解决方案

Not the prettiest in the book, but you can use the CommandManager to invalidate all commandbinding:

CommandManager.InvalidateRequerySuggested();

See more info on MSDN

这篇关于WPF - 如何强制命令通过其CommandBindings重新评估“CanExecute”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆