0

I created a WPF class library containing themes which I want to refer to and use in two WPF applications.

This is an example content, taken from my \Themes\generic.xaml file in the class library:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Style TargetType="Border">
    <Setter Property="Background">
      <Setter.Value>
        <SolidColorBrush Color="Tan"/>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

And this is the AssemblyInfo.cs file I added to the class library:

using System.Windows;

[assembly: ThemeInfo(
    ResourceDictionaryLocation.SourceAssembly,  //where theme specific resource dictionaries are located
                                                //(used if a resource is not found in the page,
                                                // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly   //where the generic resource dictionary is located
                                                //(used if a resource is not found in the page,
                                                // app, or any theme specific resource dictionaries)
)]

Nevertheless, my WPF application doesn't use the styles I've defined in the class library.

What am I missing? Why are the styles from my class library not used by my WPF application although the class library project is referenced by my WPF application?

AxD
  • 2,714
  • 3
  • 31
  • 53
  • In order to use resources from a ResourceDictionary in an external assembly, you would add that assmbly to the MergedDictionaries of a ResourceDictionary in your application, e.g. to Application.Resources. See here: https://stackoverflow.com/questions/338056/resourcedictionary-in-a-separate-assembly – Clemens Feb 07 '21 at 12:32
  • Does this answer your question? [ResourceDictionary in a separate assembly](https://stackoverflow.com/questions/338056/resourcedictionary-in-a-separate-assembly) – Orace Feb 08 '21 at 09:45
  • Is this necessary when using `\Themes\generic.xaml`? I believed this file is special in being looked up without being explicitly referenced? – AxD Feb 08 '21 at 13:00
  • https://social.msdn.microsoft.com/Forums/en-US/b56640c8-ba9e-45c5-8692-3923e96038a9/how-to-change-the-style-for-built-in-wpf-control-in-genericxaml?forum=wpf – AxD Feb 08 '21 at 13:18

1 Answers1

2

The Themes/generic.xaml naming convention is (only) used to lookup the default style(s) for any controls that are defined in the source assembly (or a specific theme assembly depending on the [ThemeInfo] attribute).

It's not used to apply styles to other controls or elements such as Border, which is defined in PresentationFramework.dll.

So to apply your Style to Border elements in your app, you need to merge the resource dictionary in your App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Themes/generic.xaml.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
mm8
  • 163,881
  • 10
  • 57
  • 88