Update2
I’ve rewritten the entire question, because some things became a lot clearer, the issue now seems to be that I created a list of DependencyProperties on a different thread than where the DependencyProperties will be used :(.
When I do some work on a BackgroundWorker the XAML bindings cause an ArgumentException
Must create DependencySource on same Thread as the DependencyObject.
I’ve got the following setup:
I’ve got a simple class that implements INotifyPropertyChanged which contains a few ints, list’s and Dictionaries.
public class Calculator : INotifyPropertyChanged
{
//Note that InstanceGroup is a dependency object
private List<InstanceGroup> instanceGroups = new List<InstanceGroup>();
public List<InstanceGroup> InstanceGroups
{
get { return instanceGroups; }
set { instanceGroups = value; }
}
// snip //
public void Calculate()
{
InstanceGroups = MyNewFilledInstanceGroup;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("instanceGroups"));
}
}
}
In a UserControl I use a BackgroundWorker to run the Calculate method, because it can take a lot of time:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate(object o, DoWorkEventArgs args)
{
lock (Calculator)
{
Calculator.Calculate();
}
};
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
In the XAML file of the UserControl I have multiple bindings to the Calculator, like {Binding Path=Calculator.InstanceGroups, Path=userControlName}.
(The overall goal is to be able to do all the work in the Calculate method on a separate thread so I can show a progress bar or something like that)
The only solution seems to be to not create DependencyProperties ( How to handle ObservableCollection<> result from a parallelized Task in MVVM? ) in a background task, this weird behavior costed me the better part of the day. But at least now I know what the problem was.