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

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

问题描述

我有一个菜单,其中每个菜单项层次结构中的有命令属性设置为的RoutedCommand 我定义。相关的的CommandBinding 提供了一个回调的评价 CanExecute 控制每个的启用状态菜单项

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 回调使用的数据发生变化,我需要的命令重新请求的结果,从我的回调,以便为这个新状态反映在用户界面。

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).

推荐答案

不是prettiest的书,但你可以使用命令管理无效所有的CommandBinding:

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

CommandManager.InvalidateRequerySuggested();

请参阅<一个更多信息href="http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.invalidaterequerysuggested.aspx">MSDN

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

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