How to ensure buttons are enabled when your window loads

We have found a problem whereby when a window loads, some of the buttons are disabled. Clicking anywhere on the window (even on the title bar) enables the button, but this doesn’t give a good user experience.

Thankfully the fix is simple. You need to find a place in the view model code where all the data has loaded, all events have been raised, and all INC properties have been set. Then you add the following lines…

if (Application.Current != null) {
  Application.Current.Dispatcher
    .Invoke(CommandManager.InvalidateRequerySuggested);
}

That will fix the issue.

Ideally you would do this in the base view model, but this requires having somewhere in there where you know all data has been loaded. If you have that, then consider yourself amongst the blessed

Be First to Comment

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.