Locating a Specific ResourceDictionary Within A MergedDictionary

Chances are you don’t really need to do this. In all probability you are latranating up the wrong woody perennial.

Answer this question: why do you want to locate a specific ResourceDictionary in the first place? Probably, like yours truly, you want to locate a specific resource within that particular ResourceDictionary. Like me you may have written some abomination like this:

return (DataTemplate)Application.Current.Resources.

Yes, enough to make you reach for the nearest brown paper bag isn’t it.

Good News: You don’t need to know the specific ResourceDictionary to locate your resource. And that’s because Dictionaries, including Merged Dictionaries are gigantic Hash Tables which means that all your resources are uniquely keyed within the Merged Dictionary itself. Which means you don’t need to know which are ResourceDictionary contains your resource. Which means you can simply write:

return (DataTemplate)Application.Current.Resources[key]

There. That feels better doesn’t it ?


Tags: , ,

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

%d bloggers like this: