views:

55

answers:

2

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:

_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.

If I set the initial value of the property to true, then the button is enabled and false it is disabled.

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.

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.

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.

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..

+4  A: 

CommandManager.InvalidateRequerySuggested() may be the answer - it tells all the commands to check whether they are enabled or not.

amaca
Perfect -- since there are only two Commands that I use, and both needed to be updated, this works perfectly.
Ian P
+1  A: 

You have to raise the CanExecuteChanged event:

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.

Merlyn Morgan-Graham