benruehl / adonis-ui

Lightweight UI toolkit for WPF applications offering classic but enhanced windows visuals
https://benruehl.github.io/adonis-ui/
MIT License
1.71k stars 143 forks source link

Menu control doesn't pick up live color theme change #73

Closed maximilien-noal closed 4 years ago

maximilien-noal commented 4 years ago

Describe the bug When changing between the light and dark color theme at runtime, the Menu control stays with the original theme that was use at the startup time of the application.

To Reproduce Use a menu with AdonisUI, and the Window Background Brush set as Layer0BackgroundBrush. Start up the app, and then change Windows' color theme.

Expected behavior It should change theme, as does the rest of the UI.

Screenshots

https://i.imgur.com/LCHlV6g.png

Additional context

The strange thing is that the text inside the menu works as expected, but its background does not.

You can check-out this repo and/or use the preview release to see it : https://github.com/maximilien-noal/RoleDDNG

benruehl commented 4 years ago

I looked at your repository and found the issue.

In your App.xaml you have the following:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/AdonisUI;component/ColorSchemes/Light.xaml" />
    <ResourceDictionary Source="pack://application:,,,/AdonisUI;component/ColorSchemes/Dark.xaml" />
    <ResourceDictionary Source="pack://application:,,,/AdonisUI.ClassicTheme;component/Resources.xaml" />
    <ResourceDictionary Source="/DataTemplates/ContentTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>

Change this to:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/AdonisUI.ClassicTheme;component/Resources.xaml" />
    <ResourceDictionary Source="/DataTemplates/ContentTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>

... and you're done.

Explanation

The way the color schemes work is that they both define colors and brushes with identical x:Key values. Each style e.g. for the menu items references the colors using those keys. When you switch the color scheme, the colors of the old scheme are removed and the colors of the new scheme are added instead. You should always include only one color scheme file. If you include both, the latter overwrites the former.

In your app this is not visible at first because you call ChangeTheme() on startup which calls ResourceLocator.SetColorScheme(). If you look at this method you can see that it searches for the first occurence of one of those color scheme files in you App.Resources in order to replace it with the other one. But because you included both files at the same time one remains in the resources and overrides the other.

maximilien-noal commented 4 years ago

Ah, I get it. That fixed it ! Thank you for your great explanation. :)