WPF 顶级菜单项根据任务启用/禁用

发布于 2024-10-06 11:36:09 字数 389 浏览 2 评论 0原文

我有一个顶级菜单项,负责刷新同一窗口中的数据网格。我当前的控制流程是:

  1. 用户单击刷新
  2. 在单击事件处理程序中,我:
    1. 通过设置 oMenuItem.IsEnabled = false 禁用菜单项。
    2. 调度一个操作来刷新网格,在该操作中,我通过设置 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:

  1. User clicks on refresh
  2. In the click event handler, I:
    1. Disable the menuitem, by setting oMenuItem.IsEnabled = false.
    2. Dispatch an action to refresh the grid and in that action, I re-enable the menuitem, by setting IsEnabled = true

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2024-10-13 11:36:09

您的意思是通过调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文