How to reference WPF style keys defined in a separate assembly in another library

Resource lookup in WPF works in a hierarchy: up the logical tree, then application resources, theme resources and finally system resources.

Theme resources can usually be accessed only implicitly (even within the assembly they are defined). This is relevant only for Styles, where the TargetType can be used as the implicit key.

To accomplish what you're trying to do, there are two options:

  • Use a ComponentResourceKey. This is a special resource key which allows referencing resources from other assemblies. For example, you can define a brush in WpfControls theme dictionary:

    <LinearGradientBrush x:Key="{ComponentResourceKey TypeInTargetAssembly=local:MyClass, ResourceId=MyBrush}">
        <GradientStop Color="Red" />
        <GradientStop Color="Blue" Offset="1" />
    </LinearGradientBrush>
    

    And then you can reference it in SpecializedControls:

    <UserControl Background="{StaticResource {ComponentResourceKey TypeInTargetAssembly=wpfControls:MyClass, ResourceId=MyBrush}}" />
    
  • Use MergedDictionaries to import a dictionary into the application resources. You can do this in the Application assembly, and when the application loads, even controls that are in SpecializedControls would be able to use these resources. You will have a problem with the design-time experience in this scenario, which you can solve by putting a fake App.xaml in SpecializedControls, that would also contain a reference to the dictionary.

Hope this helps,

Aelij.