Few people here on MSDN Wpf Forum and on Stackoverflow Forum have recently asked how to have an optional ResourceDictionary.

ResourceDictionaries are pretty easy there is nothing special about them, right? You have your colors or styles inside them and that is it? That is what I thought in first place but how to have a ResourceDictionary that is only available at design time?

Nevertheless in the end I came up with a pretty nice trick which I would like to publish to you guys.

The problem was how to optionally use ResourceDictionaries? Whaaat?

What I mean by that is what if we want one theme to be used over another when we are in design time?  It's like how to have a ResourceDictionary with an on off switch?

Is it even possible to define a ResourceDictionary that will be used only by Designer in Visual Studio to display a certain theme or certain colors?

How about having specific resources like images or even styles enabled just for Designer in Visual Studio?

The answer to all the questions above is inside this small little tricky trick.

Here is the code:

public class DesignTimeResourceDictionary : ResourceDictionary
{
    /// <summary>
    /// Local field storing info about designtime source.
    /// </summary>
    private string designTimeSource;
 
    /// <summary>
    /// Gets or sets the design time source.
    /// </summary>
    /// <value>
    /// The design time source.
    /// </value>
    public string DesignTimeSource
    {
        get
        {
            return this.designTimeSource;
        }
 
        set
        {
            this.designTimeSource = value;
            if ((bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)
            {
                base.Source = new Uri(designTimeSource);
            }
        }
    }
 
    /// <summary>
    /// Gets or sets the uniform resource identifier (URI) to load resources from.
    /// </summary>
    /// <returns>The source location of an external resource dictionary. </returns>
    public new Uri Source
    {
        get
        {
            throw new Exception("Use DesignTimeSource instead Source!");
        }
 
        set
        {
            throw new Exception("Use DesignTimeSource instead Source!");
        }
    }
}

Here is how you guys should use it:

<Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1">
   
  <Window.Resources>
    <local:DesignTimeResourceDictionary DesignTimeSource="pack://application:,,,/BlueColors.xaml"/>
  </Window.Resources>
   
    <Grid>
      <Button Background="{DynamicResource defaultBackground}"
          HorizontalAlignment="Center" VerticalAlignment="Center">click me</Button>
    </Grid>
</Window>

Just like a pretty normal ResourceDictionary but instead of Source property you set the DesignTimeSource property. That would be it?

When you open Designer you will see your specific colors being applied but when you run your project none of changes will be displayed.

Sometimes when you working in a MDA enviroment you do not have Designer available since the style comes at runtime from shell. You often end up in running application just to see if you positioned the button right. However now with this approach you will be able to use Designer as well.

To read more about IsInDesignMode property check the msdn link:

http://msdn.microsoft.com/en-us/library/system.componentmodel.designerproperties.isindesignmodeproperty(v=vs.110).aspx

This is also a pretty cool tut about IsInDesignMode property:

http://www.identitymine.com/forward/2009/09/detecting-design-time-mode-in-wpf-and-silverlight/