WPF:绑定到命令的 ContextMenu 项仅在从另一个源调用该命令后才启用。为什么会这样呢?
我有一个 ContextMenu,其项目全部绑定到命令,并在从另一个源调用任何命令之后正确启用/禁用,但在此之前,它们全部被禁用。因此,如果我运行该应用程序,所有菜单项都会被禁用,但如果我从另一个源(例如按钮)调用任何绑定命令,它们就会与 CanExecute 代码同步。我不知道如何调试这个。任何想法都会有帮助!?!
I have a ContextMenu whose items are all bound to commands and enable/disable correctly after ANY Command is invoked from another source but prior to, they are all disabled. So if I run the app, all the MenuItems are disabled but if I invoke any of the bound commands from another source (buttons, for instance) they become synchronized with the CanExecute code. I have no idea how to debug this. Any thought would be helpful!?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎是一个错误,窗口的主焦点范围内没有焦点元素。
解决方法是将 MenuItem 的 CommandTarget 绑定到主窗口。
Marco Zhou 的回答在这里:
http://social.msdn .microsoft.com/Forums/en-US/wpf/thread/7bd75a7c-eab4-4f3a-967b-94a9534a7455
Seems to be a bug where there is no focused element in the window's main focus scope.
A workaround is to bind MenuItem's CommandTarget to the main window.
Answer from Marco Zhou here:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7bd75a7c-eab4-4f3a-967b-94a9534a7455
有时您需要强制 WPF 重新评估命令是否启用。
在代码中的某个位置添加对以下内容的调用:
看看是否有帮助。
Sometimes you need to force WPF to re-evaluate whether commands are enabled or not.
Somewhere in your code, add a call to:
See if that helps.