Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think what you're asking is you want a code-behind file for a ResourceDictionary. You can totally do this! In fact, you do it the same way as for a Window:</p> <p>Say you have a ResourceDictionary called MyResourceDictionary. In your MyResourceDictionary.xaml file, put the x:Class attribute in the root element, like so:</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="MyCompany.MyProject.MyResourceDictionary" x:ClassModifier="public"&gt; </code></pre> <p>Then, create a code behind file called MyResourceDictionary.xaml.cs with the following declaration:</p> <pre><code>namespace MyCompany.MyProject { partial class MyResourceDictionary : ResourceDictionary { public MyResourceDictionary() { InitializeComponent(); } ... // event handlers ahead.. } } </code></pre> <p>And you're done. You can put whatever you wish in the code behind: methods, properties and event handlers.</p> <p><strong>== Update for Windows 10 apps ==</strong></p> <p>And just in case you are playing with <strong>UWP</strong> there is one more thing to be aware of:</p> <pre><code>&lt;Application x:Class="SampleProject.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:rd="using:MyCompany.MyProject"&gt; &lt;!-- no need in x:ClassModifier="public" in the header above --&gt; &lt;Application.Resources&gt; &lt;ResourceDictionary&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;!-- This will NOT work --&gt; &lt;!-- &lt;ResourceDictionary Source="/MyResourceDictionary.xaml" /&gt;--&gt; &lt;!-- Create instance of your custom dictionary instead of the above source reference --&gt; &lt;rd:MyResourceDictionary /&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;/ResourceDictionary&gt; &lt;/Application.Resources&gt; &lt;/Application&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload