Search code examples
wpfsilverlightsharepointmvvmtelerik-grid

asynchronous UI update from ViewModel in WPF


I saw an original question here. asynchronous UI update from ViewModel in WPF Although there was an accepted answer, the original author was asking a question I'm stumped on. This all boils down to the proper way to incorporate the Dispatcher object in a bound View model.

So...I have a view with a grid bound to a observable collection in a View Model "Cars". In the constructor of the view model I call SharePoint using QueryAsync. In the success method I'm supposed to call the Dispatcher. Possibles are >> Dispatcher.CurrentDispatcher.Invoke(),App.Current.Dispatcher.Invoke

None of these seem to work for me. I don't know if I'm supposed to send the Dispatcher to the View Model. (I don't know how to do that) or Create a Dispatcher in the View Model and assign it something.

I really don't even think I'm doing this the right way. You would think there would be some decent example code out there.


Solution

  • You could add a public reference to the Dispatcher of the UI Root Visual (your main window/user control) in the App class (App.xaml.cs), and possibly inject it when instancing your view model.

    I suggest you to use a MVVM framework like MVVM Ligh toolkit, that already solves the problem in a very elegant way, here an example: Simple example of DispatcherHelper