I already searched the forum but no asked question fits to my problem I think ...
I use a BackgroundWorker to handle a very time consuming operation. After this operation finishes a button should be set to enabled so that the user can perform another operation.
I'm using WPF and I'm following the MVVM pattern, so I have no direct access to this button. I have to call a method in the BackgroundWorker_RunWorkerCompleted event handler which sets a Property, representing the buttons enabled-state, to true.
That all works fine except one thing: The button is only redrawn after I e.g. click into the window (or maximize the window, ...). Thats very annoying and took me the whole day to get rid of this behaviour but I can't find a solution ...
The BackgroundWorker_RunWorkerCompleted event handler looks like this:
void fileLoadBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
SetButtonToEnabled(); }
Has anyone any idea how to fix this issue?
Edit:
The button is bound to a command:
<Button Name="btnLoadTargetFile" Command="{Binding Path=LoadTargetCommand}" .../>
This command is a RelayCommand as Smith stated in his blog (http://msdn.microsoft.com/en-us/magazine/dd419663.aspx) and looks like this:
public RelayCommand LoadTargetCommand
{
get
{
if (loadTargetCommand == null)
{
loadTargetCommand = new RelayCommand(param => this.OnRequestLoadFile(BusinessLogic.CustomTypes.TreeType.Target), param => this.CanLoadTarget);
}
return loadTargetCommand;
}
set { loadTargetCommand = value; }
}
this.CanLoadTarget is set to true by the SetButtonToEnabled(); method
Edit 2:
the following code 'works':
fileLoadBackgroundWorker.RunWorkerAsync(argumentList);
while(fileLoadBackgroundWorker.IsBusy)
System.Windows.Forms.Application.DoEvents();
SetButtonToEnabled();
but that is some sort of really dangerous and ugly code ...