问题描述
我有一个 Menu
,其中层次结构中的每个 MenuItem
都将其 Command
属性设置为 RoutedCommand
I'已经定义.关联的 CommandBinding
为 CanExecute
的评估提供回调,该回调控制每个 MenuItem
的启用状态.
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
.
这几乎有效.菜单项最初带有正确的启用和禁用状态.但是,当我的 CanExecute
回调使用的数据发生变化时,我需要该命令从我的回调中重新请求结果,以便在 UI 中反映这个新状态.
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.
RoutedCommand
或 CommandBinding
上似乎没有任何公共方法.
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).
推荐答案
不是书中最漂亮的,但是你可以使用CommandManager来使所有的命令绑定失效:
Not the prettiest in the book, but you can use the CommandManager to invalidate all commandbinding:
CommandManager.InvalidateRequerySuggested();
在 MSDN
这篇关于WPF - 如何强制命令通过其 CommandBindings 重新评估“CanExecute"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!