很奇怪的问题,除非鼠标被点击,Button不会重新启用 [英] Weird problem where Button does not get re-enabled unless the mouse is clicked

查看:124
本文介绍了很奇怪的问题,除非鼠标被点击,Button不会重新启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是使用WPF中的MVVM模式编写的,所有的Buttons都使用命令绑定在我的模型中执行代码。所有命令在CanExecute中都有代码来确定绑定的Button的Enabled状态。逻辑完美地工作,但在所有情况下,GUI保持在禁用状态,除非我单击GUI中的其他地方。

My app is written using the MVVM pattern in WPF, and all of my Buttons use Command bindings to execute code in my model. All commands have code in CanExecute to determine the bound Button's Enabled state. The logic works perfectly, but in all cases, the GUI remains in a disabled state unless I click somewhere else in the GUI.

例如,我有一个名为Discard糖果。当我点击这个按钮,它启动一个进程在线程池线程,设置一个bool属性称为运行。因为Discard Candy的命令的CanExecute方法看起来像这样

For example, I have a button called Discard Candy. When I click this button, it launches a process in a threadpool thread, which sets a bool property called Running to true. Since the CanExecute method for Discard Candy's command looks something like this

public bool CanExecute(object parameter)
{
  return !Running;
}

一旦过程开始,按钮将被禁用。问题是,当进程完成时,Running会设置为 false ,但GUI不会更新,即Discard Candy不会重新启用。

the button will be disabled once the process starts. The problem is that when the process is done, Running gets set to false, but the GUI doesn't update, i.e. Discard Candy doesn't get re-enabled.

然而,如果我点击GUI中的任何地方,如窗口或标题栏上,忽略糖果按钮突然被启用。所以逻辑工作,但事情发生,我只是不明白。有人可以向我解释这个行为吗?

However, if I click anywhere in the GUI, like on the window or title bar, the Discard Candy button all of a sudden gets enabled. So the logic works, but something is going on that I just don't understand. Can someone please explain this behavior to me?

EDIT - 到目前为止,它听起来像CommandManager.InvalidateRequerySuggested没有帮助人。我会给它一个镜头,但在这一刻,我有点小心。我遵循推荐的链接,并在这样做决定阅读更多关于MVVM光工具包。它听起来很不错。很好 - 有人在这里使用它,并能够确认它没有展现我迄今为止看到的问题?虽然我计划在下一个主要版本尝试MVVM光工具包。的我的应用程序,我不想重做所有的命令,我目前有到位,这就是为什么我可能从CommandManager.InvalidateRequerySuggested开始,所以我们可以在这里获得另一个数据点的有用性。

EDIT -- so far, it sounds like CommandManager.InvalidateRequerySuggested hasn't helped people. I am going to give it a shot, but at the moment am a little wary of it. I did follow the recommended links, and in doing so decided to read more about the MVVM light toolkit. It sounds very nice -- has anyone here used it and been able to confirm that it does not exhibit the problem I've been seeing so far? Although I plan to try the MVVM light toolkit in the next major rev. of my application, I don't want to redo all of the commanding that I currently have in place, which is why I'll likely start with CommandManager.InvalidateRequerySuggested so we can all get another data point here regarding it's usefulness.

EDIT#2 - 非常有趣,MVVM光工具包实际上依赖CommandManager.InvalidateRequerySuggested来支持UI禁用/重新启用命令的能力。作者说:

EDIT #2 -- very interesting, the MVVM light toolkit actually relies on CommandManager.InvalidateRequerySuggested in order to support the UI's ability to disable / re-enable commands. The author says:

严格地说,在WPF中,如果你的命令绑定到一个由CommandManager监视的控件,你不必提高CanExecuteChanged事件自己,你可以让CommandManager处理情况。也就是说,外部事件也可能改变UI的状态。让我们假设UI应该在上午9点到下午5点启用,然后禁用夜间,不触发UI,所以代码应该请求(礼貌地)CommandManager重新请求命令的状态。这是通过调用CommandManager上的方法InvalidateRequerySuggested来完成的,正如你猜到的,RelayCommand类的方法RaiseCanExecuteChanged

"Strictly speaking, in WPF, and if your command is bound to a control that is watched by the CommandManager, you shouldn’t have to raise the CanExecuteChanged event yourself. You can let the CommandManager handle the situation. That said, external events might also change the state of the UI. Let’s imagine that the UI should be enabled from 9AM to 5PM, and then disabled for the night. The user is not triggering the UI, so the code should request (politely) that the CommandManager requeries the state of the commands. This is done by calling the method InvalidateRequerySuggested on the CommandManager. And as you guessed, the method RaiseCanExecuteChanged of the RelayCommand class does just that."

推荐答案

WPF不更新命令绑定控件,除非有理由。单击GUI会使WPF刷新,以便更新可以工作。

WPF doesn't update command bound controls unless it has a reason to. Clicking on the GUI causes WPF to refresh so the update then works.

您可以通过调用 CommandManager.InvalidateRequerySuggested

You can manually cause a refresh of any command bound controls by calling CommandManager.InvalidateRequerySuggested.

这篇关于很奇怪的问题,除非鼠标被点击,Button不会重新启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆