Tuesday, March 08, 2011

InvalidateRequerySuggested on GUI Thread

I've been having issues where buttons weren't getting enabled on my WPF application, only by clicking the form would they work.

I found that I need to force the refresh using CommandManager.InvalidateRequerySuggested but that didn't work, the reason being I wasn't on the GUI Thread, I was on another thread, so here is my little helper class to do it.

public static void RaiseInvalidateRequerySuggested()
{
     Dispatcher dispatcher = null;
     if (Application.Current != null)
      {
         dispatcher = Application.Current.Dispatcher;
       }
      if (dispatcher != null && !dispatcher.CheckAccess())
       {
        dispatcher.BeginInvoke(DispatcherPriority.Normal,(Action)CommandManager.InvalidateRequerySuggested);
        }
}

Read more: Sarkie's Site