Search code examples
wpfwindows-themes

WPF Theme resources are not used


In the application I am working on a requirement is to have support for windows themes (Aero, Classic, Luna).

I have a few custom controls which I want to respect the current theme.

I have created themes-folder and placed

/themes/classic.xaml
/themes/luna.normalcolor.xaml
/themes/aero.normalcolor.xaml
/themes/generic.xaml

In the AssemblyInfo.cs of the project containing the custom controls I have defined ThemeInfo as the following:

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

When I change theme in windows, all standard controls switch theme, but my custom controls refuses to read from anything but generic.xaml

Am I missing anything obvious here?


Solution

  • Some things to check:

    1. I'm not sure if the capitalization matters, but I usually see it as "Themes/Aero.NormalColor.xaml" etc.

    2. Verify the Build Action for your XAML files is "Page". To do this, select them and press F4.

    3. The theme will not switch if your application is running. Most the native WPF control leverage the SystemColors class. Which means they may switch colors when the theme changes, but the Style/ControlTemplate won't be changed.

    If those don't help, you'd probably need to post additional code, such as how your custom control overrides DefaultStyleProperty and a sample Style.