In WPF how do I reference a static resource that is defined in a different XAML file?

Lee Warner picture Lee Warner · Sep 2, 2010 · Viewed 7.3k times · Source

In WPF how do I reference a static resource that is defined in a different XAML file? It's in the same project.

Answer

Quartermeister picture Quartermeister · Sep 2, 2010

The other XAML file will need to be a resource dictionary. You merge it into the current file using the MergedDictionaries property of the current ResourceDictionary. See Merged Resource Dictionaries on MSDN. Their example:

<Page.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="myresourcedictionary.xaml"/>
      <ResourceDictionary Source="myresourcedictionary2.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Page.Resources>

Then within that Page object you can reference static resources defined in myresourcedictionary.xaml or in myresourcedictionary2.xaml.