WPF Style and Template Resources – Order Matters

When working with WPF resources, remember to declare the resources in the correct order.

A style or template that is referenced elsewhere must be defined before it can be used. In other words, if Style1 uses BrushA as a StaticResource, then BrushA must be defined before Style1. Otherwise, you’ll get fun errors like:

Unable to cast object of type ‘MS.Internal.NamedObject’ to type ‘System.Windows.FrameworkTemplate’

It may be common knowledge, but it can be a real pain to diagnose. This is especially true if you have a large set of template and styles for an application. It gets even more fun when you have multiple themes.

Cheers.

One thought on “WPF Style and Template Resources – Order Matters

Add yours

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

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: