Assembly-wide / root-level styles in WPF class library

Try adding

Style={DynamicResource MyStyle}

You cannot use a StaticResource in this case.


This sounds like a job for theming.

  1. Add a /themes/generic.xaml ResourceDictionary to your project.
  2. Add the following to AssemblyInfo.cs: [assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
  3. ?
  4. Profit!

Any resources you add to generic will be used by all controls. Also you can make profile specific themes (Luna, Aero etc.) by including a ResourceDictionary file with the correct theme name in the themes directory.

Heres a link to more info: Create and apply custom themes


If you dont have an app.xaml, you can still load it into the appplication-level resources, but you have to write code (not xaml) to do it, similar to this...

void LoadIt()
{
     ResourceDictionary MyResourceDictionary = new ResourceDictionary();
     MyResourceDictionary.Source = new Uri("MyResources.xaml", UriKind.Relative);
     App.Current.Resources.MergedDictionaries.Add(  MyResourceDictionary )
}

check out this site for an example: http://ascendedguard.com/2007/08/one-of-nice-features-about-wpf-is-how.html


Dr. WPF (or the person formerly known as Dr. WPF) has a great post on the subject.

Here's an excerpt from the post where they create the Application object and add resources:

if (Application.Current == null)
{
    // create the Application object
    new Application();

    // merge in your application resources
    Application.Current.Resources.MergedDictionaries.Add(
        Application.LoadComponent(
            new Uri("MyLibrary;component/Resources/MyResourceDictionary.xaml",
            UriKind.Relative)) as ResourceDictionary);
}

Since my assembly is hosted via interop I had to add setting the ShutdownMode as follows and shutdown when finished:

new Application() { ShutdownMode = ShutdownMode.OnExplicitShutdown };

It worked like a charm.


Here is a simple solution for sharing resources "module-wide" across a .NET class library. Importantly, it seems to robustly preserve the XAML Designer display capabilities and behaviors in Visual Studio.

Start by adding a new C# class derived from ResourceDictionary as follows. An instance of this class will replace the default ResourceDictionary on each System.Windows.Control (or other ResourceDictionary-bearing component) in the class library that needs to see the shared resources:

ComponentResources.cs:

using System;
using System.Reflection;
using System.Windows;
using System.Windows.Markup;

namespace MyNamespace
{
    [UsableDuringInitialization(true), Ambient, DefaultMember("Item")]
    public class ComponentResources : ResourceDictionary
    {
        static ResourceDictionary _inst;

        public ComponentResources()
        {
            if (_inst == null)
            {
                var uri = new Uri("/my-class-lib;component/resources.xaml", UriKind.Relative);
                _inst = (ResourceDictionary)Application.LoadComponent(uri);
            }
            base.MergedDictionaries.Add(_inst);    //  <--  !
        }
    };
}

Be sure to replace MyNamespace and my-class-lib in the previous code snippet with (respectively) the namespace and assembly filename (without the '.dll' extension) from your own project:

Add a new ResourceDictionary XAML file to your class library project. Unlike as for 'Application' assemblies, there is no option for this in the Visual Studio UI, so you'll have to do it manually. This will contain the resources you want to share across the whole class library:

$(ProjectDirectory)\Resources.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MyNamespace">

    <!-- define the resources to be shared across the whole class library here -->

    <!-- example for demonstration -->
    <Style TargetType="Rectangle">
        <Setter Property="Fill" Value="Red" />
    </Style>

<ResourceDictionary>

Set the Build Action to "Page" and be sure the File Properties information looks like this:

enter image description here

Finally, go to the XAML files in the project that need to reference the shared resources, and replace the default ResouceDictionary (typically, on the Resources property of the XAML root element) with a ComponentResources instance. This will hold each component's private resources, and as you can see in the C# code at the top (the ComponentResources constructor), each of these various instances will all attach the same module-wide shared singleton ResourceDictionary as a "merged dictionary." Do this for every control that should see the shared resources, even those that have no private resources of their own. Of course, you can also skip this step to exclude certain controls from sharing as needed. For example:

<UserControl x:Class="MyNamespace.UserControl1"
             xmlns="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyNamespace">

    <UserControl.Resources>
        <local:ComponentResources>
            <!-- Keep any existing non-shared resources here from before -->
            <!-- Can be empty if this control has no private resources -->
        </local:ComponentResources>
    </UserControl.Resources>

    <!-- to demonstrate that the Style in the above example is effective... -->
    <Grid>
        <Rectangle Width="10" Height="10" />
    </Grid>

</UserControl>

As advertised, it works great, including in the XAML Designer...

enter image description here