If you have in your WPF project an ResourceDictionary that you use to define resources you can create an instance of it from code like this:

ResourceDictionary res = Application.LoadComponent(
 new Uri("/WpfApplication7;component/RedRibbonBar.xaml", 
 UriKind.RelativeOrAbsolute)) as ResourceDictionary;

Where WpfApplication7 is name of your assembly and RedRibbonBar.xaml is name of your ResourceDictionary.

Another less known way of doing same thing is defining the code-behind for the resource dictionary. I have not found an automated way of doing this in VS.NET 2010 so I do it manually.

First you add x:Class to your ResourceDictionary definition like so:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 x:Class ="WpfApplication7.RedRibbonBar">

Then you add class to your project that holds code-behind and you name it RedRibbonBar.xaml.cs where you change the name to whatever your resource dictionary name is.

The code behind class looks like so:

partial class RedRibbonBar: ResourceDictionary
   public RedRibbonBar()

Note that you need to change RedRibbonBar I used here to the name of your ResourceDictionary. Then you create simply new instance of your dictionary like so:

ResourceDictionary res = new RedRibbonBar();
Professional looking applications made easy with DotNetBar for WinForms, Silverlight and WPF User Interface components. Click here to find out more.

Related posts:

  1. BitmapImage.ImageSource image loading – correct syntax for URI
  2. Dreaming In Code Review
  3. How to learn to create controls from code using InitializeComponent() method
  4. Is your code quality connected to time?
  5. Code-word: Framework