WPF - 如何强制命令通过其CommandBindings重新评估“CanExecute” [英] WPF - How to force a Command to re-evaluate 'CanExecute' via its CommandBindings
问题描述
我有一个菜单
其中每个 MenuItem
在其层次中有 code>属性设置为
RoutedCommand
我已经定义。相关的 CommandBinding
为 CanExecute
的评估提供回调,控制每个 MenuItem
。
这几乎工作。菜单项最初产生正确的启用和禁用状态。但是当我的 CanExecute
回调使用的数据更改,我需要重新请求一个结果从我的回调的命令为了这个新的状态反映在UI。
RoutedCommand
或 CommandBinding $上似乎没有任何公开方法请注意,当我单击或键入控件(我猜它是在输入触发,因为鼠标悬停不会再次使用回调函数。)
不是本书中最漂亮的,但可以使用CommandManager来使所有命令绑定无效:/ p>
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屋!