I just noticed that when changing bound properties in my ViewModel
(MVVM) from a background worker thread I do not get any exceptions and the view is updated correctly. Does this mean I can safely rely on wpf databinding marshalling all changes in the ViewModel
to the UI Thread? I think I have read somewhere that one should make sure (in the ViewModel
) that INotifyPropertyChanged.PropertyChanged
is fired on the UI thread. Has this changed in 3.5 or something?
相关问题
- VNC control for WPF application
- How to let a thread communicate with another activ
- How do I bind a DataGridViewComboBoxColumn to a pr
- Why it isn't advised to call the release() met
- WPF Binding from System.Windows.SystemParameters.P
Yes for scalars, no for collections. For collections, you'll need a specialized collection that marshals for you, or manually marshal to the UI thread yourself via the
Dispatcher
.You may have read that
INotifyCollectionChanged.CollectionChanged
must fire on the UI thread, because it's simply not true ofINotifyPropertyChanged.PropertyChanged
. Below is a very simple example that proves WPF marshals property changes for you.Window1.xaml.cs:
Window1.xaml:
I believe that with 2.0 and previous incarnations of .NET you would have received an InvalidOperationException due to thread affinity when executing the aforementioned example (link posted by bitbonk is dated 2006).
Now, with 3.5, WPF does appear to marshal background thread property changes onto the dispatcher for you.
So, in short, depends which version of .NET you're targetting. Hopefully that clears up any confusion.
One of my fellow Lab49'ers blogged about it here in 2007:
http://blog.lab49.com/archives/1166