Control does not refresh after CanExecute-Result does change

[亡魂溺海] 提交于 2019-12-10 13:49:25

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/662525/control-does-not-refresh-after-canexecute-result-does-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!