.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Binding Multiple UI's to the same object
 

Binding Multiple UI's to the same object

I'm getting the dreaded "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread." exception.

Essentially I am using a background worker to update a collection so I have been using Dispatcher.BeginInvoke(...) and life is good. But now I have a situation where I have multiple windows open and they're both bound to the same (static) object. If I add or remove an item from the collection they are both bound to, they both have different Dispatchers so it's impossible for me to use the "UI Thread" to update the collection since the UI thread of one window is not the UI thread of the other. What is the solution here? Is there no way to solve this other than to give each window their own collections and synchronize the two?


justncase80
The "only" way I am aware of to enable safe mutil-threaded collection data binding in WPF is the solution suggested by Beatriz Costa in the following thread:
http://www.beacosta.com/blog/?p=34

For your scenario, I am afraid that you need to use seperate copy of ObservableCollection instance, and each of which is running inside its own dispatcher.

Hope this helps


Marco Zhou
I am not sure that this is true that the various windows use different UI threads. But even in that case you can create your own Dispatcher in the Application class and use it to synchronize these threads access.

--
GL, Roman S. Golubin (RA1OGE/3)
Roman S. Golubin
Denis Vuyka
Yeah, I just changed the property I am binding to to be an array for now and I am calling OnPropertyChanged("Spheres"), which causes the whole list to refresh and fixes this problem. The problem seems to be coming from, specifically, the ObservableCollection<T> class. Most of these examples I have seen involve a single UI binding to a collection with multiple workers changing the collection. My case is the reverse, multiple UI's binding to the collection with one worker.

Databinding doesn't seem to care if you change properties in a background thread it's only when you change a collection. I will probably try to create this ThreadSafeObservableCollection<T> in the previous link since refreshing an entire Array is a very bad way to solve the problem.
justncase80
I tried the thread safe way and I'm still getting the error, here is a screen shot:

Exception on insert




You can see above that the dispatcher is actually allowing access yet throwing an an exception saying it's not from the dispatcher thread. If I only have one window open then everything works perfectly fine but if I have two then it blows up, which is leading me to believe that there are two different dispatchers attached to this collection and no matter which dispatcher I am using to change the collection it is the wrong one.

So my question is how can I change a collection with multiple UI threads bound to it?
I tried the thread safe way and I'm still getting the error, here is a screen shot:




You can see above that the dispatcher is actually allowing access yet throwing an an exception saying it's not from the dispatcher thread. If I only have one window open then everything works perfectly fine but if I have two then it blows up, which is leading me to believe that there are two different dispatchers attached to this collection and no matter which dispatcher I am using to change the collection it is the wrong one.

So my question is how can I change a collection with multiple UI threads bound to it?
justncase80
Can you give an example of how you create two windows with different UI threads? I can not repro this.
But if two different windows created in a single thread, then it works all fine. I wrote a small example that works. See http://grominc1.valuehost.ru/ThreadSafeCollectionTest.zip . Maybe it helps.
Roman S. Golubin
The "only" way I am aware of to enable safe mutil-threaded collection data binding in WPF is the solution suggested by Beatriz Costa in the following thread:
http://www.beacosta.com/blog/?p=34

For your scenario, I am afraid that you need to use seperate copy of ObservableCollection instance, and each of which is running inside its own dispatcher.

Hope this helps


Marco Zhou
justncase80 wrote:
I tried the thread safe way and I'm still getting the error, here is a screen shot:

Exception on insert

[skip...]

So my question is how can I change a collection with multiple UI threads bound to it?

This exception is due to the incomplete solution to the problem. The fact is that the interface INotifyCollectionChanged event just need to be redirected to the subscriber thread. I found a blog which contains second part of the solution to the problem. Look at http://farazmasoodkhan.wordpress.com/2008/03/09/data-binding-observablecollection-can-be-update-from-other-thread/
Maybe this will help.

Roman S. Golubin

You can use google to search for other answers

Custom Search

More Threads

• WPF changing items in a ListCollectionView
• Toolbar and mouseover style
• Databinding to the same collection in two different windows
• Access a dependency property attached to a bound property's containing object from a binding expression
• Scam??
• Unloaded doesn't fire when window is closing.
• Adding common event handlers
• Scaling visuals
• HierarchicalDataTemplate usage
• BitmapImage.StreamSource from encrypted image problem.