Styles from generic.xaml are not applied

generic.xaml, styles, themes, wpf

Solution

To apply default styles in Themes\Generic.xaml in my custom controls library I decided to seek inspiration from a well established open source controls library (MahApps). This project gives you a really good example of how to structure and layout a custom controls library.

Cut a long story short, to get your default styles in Themes\Generic.xaml you need to include the following in the AssemblyInfo.cs file in your custom controls library:

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

In my case, my custom controls AssemblyInfo.cs looked something like:

using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Markup;

[assembly: AssemblyCopyright("...")]
[assembly: ComVisible(false)]

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0.0")]
[assembly: AssemblyTitleAttribute("...")]
[assembly: AssemblyDescriptionAttribute("")]
[assembly: AssemblyProductAttribute("...")]
[assembly: AssemblyCompany("...")]

Problem

I made a class library assembly in which I created custom controls, and I defined the default styles in the generic.xaml file. It seems this is a quite common problem, as long as many people are posting about it. However I couldn't find any useful answer for my case. - the generic.xaml is in the Themes folder. - the generix.xaml file Build Action is set to Page. - the ThemeInfo is properly defined in my AssemblyInfo.cs. In my test application, if I manually merge the generic.xaml file from my custom controls assembly into the application App.xaml file like this: ``` <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/MyControlsAssembly;component/Themes/generic.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> ``` then the custom controls are properly themed, but if I do not manually merge the generic.xaml, the controls appear with the default Windows theme. Could you please tell me what am I forgetting and/or doing wrong ? Additional info: My ThemeInfo assembly attribute is defined as follow: `[assembly: ThemeInfo(ResourceDictionaryLocation.SourceAssembly, ResourceDictionaryLocation.SourceAssembly)]` (Note: the result is just the same with any combination of parameters for the ThemeInfo attribute) There are two others .xaml files beside the generic.xaml file in the Themes folder. - There is a subfolder in the Themes folder that itself contains another .xaml file.

Original source