July 28, 2014
Gets a collection of the ResourceDictionary dictionaries that constitute the various resource dictionaries in the merged dictionaries.
Assembly: System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.
<ResourceDictionary> <ResourceDictionary.MergedDictionaries> oneOrMoreResourceDictionaryElements </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
Dictionaries are merged by adding a ResourceDictionary to the generic collection referenced by . A merged ResourceDictionary does not have resource elements defined within it in markup. Specifying resource elements within a ResourceDictionary that also has Source specified is not supported. 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 specified as a URI enables the merged dictionary to come from an external source, such as "loose XAML" that is packaged with the XAP by the Content build action but not compiled into the DLL. When used as a merged dictionary source, it is illegal for a XAML file that has a ResourceDictionary root to specify x:Class.
Lookup behavior within the 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 to implement a deliberate fallback sequence, where the last-in-sequence 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 collection.
For example, consider the following XAML:
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="rd1.xaml"/> <ResourceDictionary Source="rd2.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
<!--rd1.xaml--> <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="maindialogbrush" Color="Orange"/> </ResourceDictionary>
<!--rd2.xaml--> <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="maindialogbrush" Color="LimeGreen"/> </ResourceDictionary>
In this example, if a resource request is made for the resource with the key maindialogbrush, 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 collection dictionaries, then the resource that is returned will come from the primary dictionary. In other words, the primary dictionary overrides any considerations of the lookup order between multiple merged dictionaries in the same set.