I'm using the ICommand interface to perform binding on a couple of buttons in my application, the Run and Close button. I'm using a helper method that was mentioned in this question to hook up the ICommand to a delegate as follows:
我正在使用ICommand接口对我的应用程序中的几个按钮执行绑定,即“运行”和“关闭”按钮。我正在使用此问题中提到的帮助方法将ICommand连接到委托,如下所示:
_someCommand = new DelegateCommand(this.DoSomething, this.CanDoSomething);
private ICommand _someCommand;
public ICommand SomeCommand
{
get { return _someCommand; }
}
private void DoSomething(object state)
{
// do something here
}
private bool CanDoSomething(object state)
{
// return true/false here is enabled/disable button
}
This appears to work just fine as, in my implementation, CanDoSomething returns the value of a property that I have in my application.
这似乎工作正常,因为在我的实现中,CanDoSomething返回我在我的应用程序中的属性的值。
If I set the initial value of the property to true, then the button is enabled and false it is disabled.
如果我将属性的初始值设置为true,则启用该按钮,并禁用它。
I have a series of events that are raised from a BackgroundWorker
in the application layer back to the ViewModel that change the value of the property to true or false based on the current state of the application.
我有一系列事件从应用程序层中的BackgroundWorker引发回ViewModel,它根据应用程序的当前状态将属性值更改为true或false。
The current problem I'm having is that the button is not "re-enabling" when I set the value to true after the work has completed. If I click somewhere within the window, it will update. So, therefore, I'm thinking than manually refreshing the window will solve my problem, at least for the interim. This feels a bit gross to do it this way, but I'm kind of at a loss for what else I could try.
我当前遇到的问题是,当我在工作完成后将值设置为true时,按钮不会“重新启用”。如果我单击窗口中的某个位置,它将更新。所以,因此,我认为手动刷新窗口将解决我的问题,至少在过渡期间。以这种方式做这件事感觉有点粗糙,但我还是有点不知所措。
If anyone has any suggestions, I'm all ears.
如果有人有任何建议,我会全力以赴。
Thanks for the help!
谢谢您的帮助!
Ian
伊恩
Edit -
编辑 -
A little bit more information on the application itself. It uses a background worker in the application thread to handle the "work". The application is a simple utility to manage the creating of tables and loading of data into the tables. We use a lot of pre-defined SQL scripts to setup our test environment, so this is a simple utility that allows us to do that sort of thing based on parameters provided by the user in the UI.
关于应用程序本身的更多信息。它在应用程序线程中使用后台工作程序来处理“工作”。该应用程序是一个简单的实用程序,用于管理表的创建和将数据加载到表中。我们使用大量预定义的SQL脚本来设置我们的测试环境,因此这是一个简单的实用程序,它允许我们根据用户在UI中提供的参数来执行此类操作。
Hopefully that helps, because when I re-read my question it read as if I were doing everything in the UI thread, which is not the case. Progress reports are raised back up to the UI thread and everything is updated as expected, except the button..
希望这会有所帮助,因为当我重新阅读我的问题时,它读起来好像我在UI线程中做了所有事情,事实并非如此。进度报告被提升回UI线程,除了按钮之外,所有内容都按预期更新。
2 个解决方案
#1
4
CommandManager.InvalidateRequerySuggested() may be the answer - it tells all the commands to check whether they are enabled or not.
CommandManager.InvalidateRequerySuggested()可能是答案 - 它告诉所有命令检查它们是否被启用。
#2
1
You have to raise the CanExecuteChanged event:
您必须引发CanExecuteChanged事件:
http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.canexecutechanged.aspx
http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.canexecutechanged.aspx
This may be more useful than the other answer in cases where you know you should re-evaluate a single control, and re-evaluating all the controls would be costly. The other answer is simpler if you don't have a case like that, though.
在您知道应重新评估单个控件的情况下,这可能比其他答案更有用,并且重新评估所有控件的成本会很高。如果你没有这样的案例,那么另一个答案就更简单了。
#1
4
CommandManager.InvalidateRequerySuggested() may be the answer - it tells all the commands to check whether they are enabled or not.
CommandManager.InvalidateRequerySuggested()可能是答案 - 它告诉所有命令检查它们是否被启用。
#2
1
You have to raise the CanExecuteChanged event:
您必须引发CanExecuteChanged事件:
http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.canexecutechanged.aspx
http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.canexecutechanged.aspx
This may be more useful than the other answer in cases where you know you should re-evaluate a single control, and re-evaluating all the controls would be costly. The other answer is simpler if you don't have a case like that, though.
在您知道应重新评估单个控件的情况下,这可能比其他答案更有用,并且重新评估所有控件的成本会很高。如果你没有这样的案例,那么另一个答案就更简单了。