WPF ResourceDictionary Precedence


There doesn’t seem to be a huge amount of information available on the precedence of ResourceDictionary entries in XAML. Knowing which styles will be used when you have more than one dictionary containing styles with the same name is clearly necessary.

According to the official MSDN documentation:

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. Although a resource key must be unique within any individual dictionary, a key can exist multiple times in a 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 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. If a key is defined in the primary dictionary and also in a dictionary that was merged, then the resource that is returned will come from the primary dictionary. These scoping rules apply equally for both static resource references and dynamic resource references.

This can come in handy if you need to override styles from a 3rd party Resource Dictionary within your own – provided your dictionary is referenced after the original, everything works nicely.

, , , , ,

  1. Leave a comment

Leave a comment