ResourceDictionary.MergedDictionaries Property

May 11, 2014

Gets a collection of the ResourceDictionary dictionaries that constitute the various resource dictionaries in the merged dictionaries.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

public PresentationFrameworkCollection<ResourceDictionary> MergedDictionaries { get; }
<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
    oneOrMoreResourceDictionaryElements
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

XAML Values

oneOrMoreResourceDictionaryElements

One or more explicit ResourceDictionary object elements. Each such ResourceDictionary should specify a Source value as its only attribute, and should not attempt to specify any keyed child content itself. Also, do not specify an x:Key.Attribute on the merged dictionary ResourceDictionary object elements.

Property Value

Type: System.Windows.PresentationFrameworkCollection<ResourceDictionary>
A collection of the ResourceDictionary dictionaries that constitute the various resource dictionaries in the merged dictionaries.

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. 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 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 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 MergedDictionaries 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 MergedDictionaries 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 MergedDictionaries set.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft