This project has moved and is read-only. For the latest updates, please go here.

CanExecute not updating UI when called from background thread

May 7, 2013 at 5:20 PM
Hi Laurent

I appologies if this has been asked before.

I am having an issue with my WPF application (using MVVM Light), where the CanExecute element of the RelayCommand is not updating my UI; this is appears to be becuse it is being updated from a background thread.

Is this something that can be overcome using the DispatcherHelper, if so how?

Many Thanks

May 9, 2013 at 8:07 PM
Edited May 9, 2013 at 8:15 PM
OK, so I have figured out a work around.

Fortunately, the condition on my background thread that I am using to determine the CanExecute state, has an associated event.

I hooked into the event on my ViewModel using a lamda expression for each instance of the object in question, and used the dispatcher helper to call the RaiseCanExecuteChanged on the UI thread for each associated command.
DispatcherHelper.CheckBeginInvokeOnUI(() => { [MyCommand].RaiseCanExecuteChanged(); });
In future, if an event is not available I will consider using the Messenger.

This works but feels a little convoluted; is there a better way of doing this?

Many Thanks