WPF - How to force a Command to re-evaluate #39;CanExecute#39; via its CommandBindings(WPF - 如何强制命令通过其 CommandBindings 重新评估“CanExecute)
问题描述
我有一个 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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:WPF - 如何强制命令通过其 CommandBindings 重新评估“CanExecute"


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04