Wpf mvvm binding List to Listbox from background thread?

Take a look at the DispatcherNotifiedObservableCollection that Cinch implements. It will automatically use the Dispatcher if need be. Read this article to see the code for it.

Take a look at the DispatcherNotifiedObservableCollection that Cinch implements. It will automatically use the Dispatcher if need be. Read this article to see the code for it: codeproject.com/KB/WPF/CinchIII.aspx#ObsCol.

You need to update the observable collection from the dispatch thread. Something like (not technically/syntactically correct, but psuedo-close) Dispatcher. BeginInvoke( () => theList.

Add( theThing ) ).

I'm no sure I quite follow this. The Dispatcher is in my view not my viewModel. In MVVM shouldn't my ViewModel be independent from my UI?

– poco May 12 at 20:15 yes, probably. BUT... the collection change notifications that the UI is depending on have to come from the dispatch thread. – John Gardner May 12 at 21:08.

A better way to achieve this functionality, is expose the Model via a property in the ViewModel, and bind the ListBox to Model.ObservableList. And use John's suggestion when adding and removing items to this list. This will not require you to copy the values from list to an observablecollection.So your viewModel code would like public class ViewModel { public Model Model { get { return _model; } } } The binding in xaml: where the ObservableList property returns your ObservableCollection.

As all UI elements in WPF have thread affinity (to the Dispatcher thread), any updates to the UI should be marshaled via the Dispatcher. You can marshal calls via the Dispatcher, using Dispatcher.Invoke.In this case, your view, viewModel and Model reside in the Dispatcher thread, so any call to update the UI via the viewModel or Model, would require you to call Dispatcher.Invoke.

I wouldn't say that it is necessary better - since now your ViewModel is exposing much more than it needs to. Anything interacting with it can now access everything in the Model. – Matt West May 12 at 21:52 True, however in MVVM since Views and ViewModels have a one-to-one relationship, I think it's an acceptable risk (what's acceptable and what's not I guess depends on your use case).

When I have used this pattern, I ensure that the models are designed such that only relevant properties are exposed. – Hasanain May 12 at 22:25 1 Why would you need one-to-one relationship between view and viewmodel in MVVM? There is nothing stopping you from reuse.

I can see plenty of use-cases where a viewmodel can be reused for different views. – Daniel May 13 at 7:58.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions