When binding to a property in Silverlight 3 and 4, the Windows Phone 7 version silverlight and in WPF 3.5 and 4 will property change notifications always be marshalled to the UI thread? Are you aware of any scenario where I can not rely on that and would have to do the marshalling in my code?
2 Answers
I did some experiments...
INotifyPropertyChanged
If you make a change to a property from a background thread, and it fires INotifyPropertyChanged from that background thread, and the property is databound, then...- WPF: it works (i.e. the databinder marshals it to the UI thread)
- Silverlight5 and WinRT: it fails (i.e. the databinder doesn't marshal)
- Phone: I assume it's the same as Silverlight, but haven't tried.
DependencyProperty
What if the property is a dependency property rather than INotifyPropertyChanged? What if you alter this property from a background thread? Well, I haven't done any experiments, but I read that it doesn't do any marshalling.INotifyCollectionChanged (e.g. ObservableCollection)
If you add/remove elements in an ObservableCollection from a background thread, and the collection is databound to a listbox or similar, then what happens?- WPF: As of WPF4.5, you can use BindingOperations.EnableCollectionSynchronization(collection, new object()); and it will marshal correctly. However, prior to WPF4.5, it's as Pavlo said.
- Silverlight, WinRT: again the same as Pavol said.

- 25,172
- 5
- 72
- 123

- 2,160
- 1
- 20
- 25
Yes, collections. When you bind to an observable collection and you change it from a non-UI thread you will get an exception. You will have to marshal the collection change to the UI thread.

- 20,498
- 3
- 58
- 71
-
Can you confirm that apart from collections I am safe in all frameworks mentioned above? – bitbonk Jan 13 '11 at 09:46
-
I can say for sure only for WPF. Haven't checked with Silverlight. – Pavlo Glazkov Jan 13 '11 at 12:24