Lets say I have some ResourceDictionary's in my Application.xaml defined as so:
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="brushResources"/>
<ResourceDictionary x:Name="graphicsResources"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
I would have assumed I could just simply access the dictionary in the code behind like so:
brushResources.MergedDictionaries.Add(someNewBrushes)
graphicsResources.MergedDictionaries.Add(someNewGraphics)
But this does not seem to be the case, is this the wrong approach to dealing with ResourceDictionary? I would really enjoy the ability to dynamically load resources with MEF, and this seems like a stumbling block to me.
EDIT
Fixed example code to show the dictionaries are actually in the MergedDictionaries
You can use
FrameworkElement.TryFindResource
. If you are in a window code behind, do this:Returns null if the resource isn't found.
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.tryfindresource.aspx
Since you cannot define a key nor a name for such resource, here's what you need to do:
Make your dictionary a resource :
Load your dictionary, keep a reference to it and do whatever you have to do with it.
Then add it to your application :
(from the docs Merged Resource Dictionaries)
This exactly replicates the original behavior but now you can access the dictionary.
For design-time you will certainly want the default behavior of having the dictionaries declared in XAML, what you can do then at run-time is to delete all the dictionaries and reload them using the above method.