Dictionaries are merged by adding a ResourceDictionary to the generic collection referenced by MergedDictionaries. A merged ResourceDictionary does not have resource elements defined within it in markup. Instead, the merged dictionary is a ResourceDictionary with no markup child elements defined (or with no elements added through code), but with a URI specified for Source. The Source designation enables the merged dictionary to come from an external source, such as "loose XAML" that is packaged with the XAP by the Page or Content build action but not compiled into the DLL (as other page-level XAML is by typical build actions). When used as a merged dictionary source, it is illegal for a XAML file that has a ResourceDictionary root to specify x:Class.
Specifying resource elements within a ResourceDictionary that also has Source specified is not supported.
Lookup behavior within the MergedDictionaries collection searches the last-added ResourceDictionary first, and the search stops as soon as a requested key is found. In other words, the retrieval logic from the collection of merged resource dictionaries is last in, first out.
Duplicate keys across the collection of merged dictionaries are not illegal. In fact, you can use multiple merged ResourceDictionary sources as part of a deliberate fallback sequence where the last referenced ResourceDictionary contains the fallbacks in case no preceding ResourceDictionary defined the key.
Merged Dictionary Lookup
Resources in a merged dictionary occupy a location in the resource lookup scope that is just after the scope of the main resource dictionary they are merged into. A resource key must be unique within any individual dictionary (including within any individual ResourceDictionary that is the source for one of the merged dictionaries). However, a key can exist multiple times in the combined set of merged dictionaries. In this case, the resource that is returned will come from the last dictionary found sequentially in the MergedDictionaries collection.
If the MergedDictionaries collection definition was defined in XAML, then the order of the merged dictionaries in the collection is the order of the elements as provided in the markup, and lookup uses the last ResourceDictionary in that order first. For example, consider the following XAML:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="rd1.xaml"/>
<ResourceDictionary Source="rd2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<!--rd1.xaml, xmlns omitted-->
<ResourceDictionary>
<SolidColorBrush x:Key="maindialogbrush" Color="Orange"/>
</ResourceDictionary>
<!--rd2.xaml, xmlns omitted-->
<ResourceDictionary>
<SolidColorBrush x:Key="maindialogbrush" Color="LimeGreen"/>
</ResourceDictionary>
In this example, if a resource request is made for the resource with the key keymaindialogbrush,the returned brush will be the color LimeGreen, because rd2.xaml is the last defined merged dictionary.
If a key is defined in the primary dictionary and also in one or more MergedDictionaries collection dictionaries, then the resource that is returned will come from the primary dictionary. Note that this entails that any merged dictionary is prior to the Application..::.Resources in the progression.