Must create DependencySource on same Thread as the DependencyObject.

Stumped me for a bit because I wasn’t expecting this as a result of the change I made. I was creating a colour in my ViewModel like this:

private readonly Brush[] colours = new[] { Brushes.Black, Brushes.DarkBlue };

But then wanted to use some more funky colours so changed it to:

private readonly Brush[] colours = new[] { new SolidColorBrush(Color.FromRgb(0x04, 0x81, 0x9E)), new SolidColorBrush(Color.FromRgb(0xEC, 0xFC, 0x00)) };

Because I was no longer referencing the static brushes and instead creating them when this class is instantiated (which happened to be on a non UI thread), this caused the above ArgumentException (which had a stack trace and values that really didn’t point to where the error actually occured).

Fixing it meant doing it properly 🙂 I created the brushes in my View and used Style triggers to choose the appropriate brush based on a value in my ViewModel.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s