Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 345
Lee Warner Avatar asked Sep 02 '10 21:09

Lee Warner


People also ask

What is a static resource WPF?

Static Resource - Static resources are the resources which you cannot manipulate at runtime. The static resources are evaluated only once by the element which refers them during the loading of XAML.

How do I add resources to XAML dictionary?

Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Here, you define a resource dictionary in a separate XAML file called Dictionary1.

How do I add one XAML to another XAML?

now, you can use this document (reference this document) with the tag <RecourceDictionary source="MyXAMLDocument. XAML"/> in the "Recource" tag from any container´s. a small sample for using a ResourceDicnionary. xaml in a window tag.


1 Answers

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.

like image 186
Quartermeister Avatar answered Oct 05 '22 19:10

Quartermeister