WPF 顶级菜单项根据任务启用/禁用
我有一个顶级菜单项,负责刷新同一窗口中的数据网格。我当前的控制流程是:
- 用户单击刷新
- 在单击事件处理程序中,我:
- 通过设置
oMenuItem.IsEnabled = false
禁用菜单项。 - 调度一个操作来刷新网格,在该操作中,我通过设置
IsEnabled = true
重新启用菜单项
- 通过设置
问题是,即使刷新被禁用,用户也可以单击刷新,就像单击一样排队。当操作返回时,它会继续处理剩余的“排队”点击。我期望的是:菜单项被禁用时的所有点击都会被忽略,只有当它被启用时,点击才会被确认。
奇怪的是,如果我只是禁用它而不启用它,它就会保持这种状态,即它被禁用。wpf,
I have a top-level menu item which is responsible for refreshing a datagrid in the same window. My current control flow is:
- User clicks on refresh
- In the click event handler, I:
- Disable the menuitem, by setting
oMenuItem.IsEnabled = false
. - Dispatch an action to refresh the grid and in that action, I re-enable the menuitem, by setting
IsEnabled = true
- Disable the menuitem, by setting
The problem is that the user can click refresh even when it's disabled and it's as if the clicks get queued up. When the action returns it goes on to process the remaining, "queued-up" clicks. What I expect is: all clicks while the menuitem is disabled are ignored and only when it's enabled, the clicks are acknowledged.
The weird thing is that if I just disable it and never enable it it stays that way, i.e., it is disabled.wpf,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的意思是通过调用 Dispatcher.BeginInvoke() 或某种其他类型的异步操作来“调度操作”?
无论如何,在这两种情况下,您都可以获得操作(DispatcherOperation 或 IAsyncResult)的“句柄”,并在分派操作时将其存储为字段。完成后 - 将此字段设置为空。
在菜单项的单击事件处理程序中检查此字段。如果它为空,则意味着可以安全地启动操作。如果它不为空 - 立即返回并且不执行任何操作。
还有一些与您的问题无关但很重要的事情 - 为什么不使用命令?这样您就不需要处理事件处理和启用/禁用。当然,命令可以通过多种方式调用(例如,用户使用键盘从菜单中选择命令并按 Enter 键。不涉及鼠标单击,但应与单击菜单项执行相同的操作)。
亚历克斯.
"Dispatch an action" you mean by calling Dispatcher.BeginInvoke() or some other kind of async opetation?
Anyway, in both cases you can get a "handle" to the operation (DispatcherOperation or IAsyncResult) and store it as a field when you dispatch your operation. When it completes - set this field to null.
In the click event handler of the menu-item check this field. If it's null it means it is safe to start the operation. If it is not null - return immediately and do nothing.
And something not related to your question but important - why not use Commands? That way you don't need to play with event handling and enabling/disabling. And of course commands can be invoked by multiple means (for example - the user selected the command from the menu using the keyboard and pressed Enter. No mouse clicks involved, but should do the same as clicking the menu item).
Alex.