Control does not refresh after CanExecute-Result does change
Asked Answered
D

1

5

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.

Discography answered 19/3, 2009 at 14:59 Comment(0)
D
9

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();

Discography answered 19/3, 2009 at 14:59 Comment(1)
Thank you for the answer. I added the CommandManager.InvalidateRequerySuggested() method in the RunWorkerCompleted event handler and worked like a charm!Synthetic

© 2022 - 2024 — McMap. All rights reserved.