WPF - 如何强制命令通过其 CommandBindings 重新评估“CanExecute"

WPF - How to force a Command to re-evaluate #39;CanExecute#39; via its CommandBindings(WPF - 如何强制命令通过其 CommandBindings 重新评估“CanExecute)
本文介绍了WPF - 如何强制命令通过其 CommandBindings 重新评估“CanExecute"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Menu,其中层次结构中的每个 MenuItem 都将其 Command 属性设置为 RoutedCommand I'已经定义.关联的 CommandBindingCanExecute 的评估提供回调,该回调控制每个 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.

RoutedCommandCommandBinding 上似乎没有任何公共方法.

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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)