views:

885

answers:

1

Hello

in my window I have buttons for load and save methods. I use CommandBinding and the save-button has a CanExecute property to keep the user from saving the data before it is loaded.

The CanExecute-Methode is connected to a simple bool value called "canSaveXML"

private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (canSaveXML == false)
    {
        e.CanExecute = false;
    }
    else
    {
        e.CanExecute = true;
    }
    e.Handled = true;
}

My intention is to set canSaveXML = true after the data was loaded but the control does not refresh after the value did change. I did some reading and found out I have to call CommandManager.InvalidateRequerySuggested. I do that now and my code looks like this.

canSaveXML = true;
CommandManager.InvalidateRequerySuggested();

But the control (button) still does not refresh. I still is disabled untill I trigger anything on the UI or minimixe/maximize the window. After I did that the button is enabled.

What is wrong here?

In a MSDN sample CommandManager.InvalidateRequerySuggested is called with a dispatchertimer again and again but I refuse to believe that would be the only solution.

+3  A: 

Ok, I found out myself.

canSaveXML = true;
CommandManager.InvalidateRequerySuggested();

was code inside a background worker. Not good. You have to tell the dispatcher of the window to invoke CommandManager.InvalidateRequerySuggested();

Holli
Thanks, you save me a lot of headaches.
Jonathan Allen