Search code examples
c#wpfxamlstaticresource

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


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


Solution

  • 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.