I am using the ThemeManager
provided by the ControlzEx
library to enable switching between themes in my application.
This works as expected when style properties are set directly on a control, for example:
MainWindow.xaml
<Window Background="{DynamicResource WhiteBrush20}">
If I then call ThemeManager.ChangeTheme()
the window background changes as expected.
However, if I try to use this same approach with a global style defined in a resource dictionary, like this:
Controls.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Window}">
<Setter Property="Background"
Value="{DynamicResource WhiteBrush20}" />
</Style>
</ResourceDictionary>
Nothing happens when ThemeManager.ChangeTheme()
is called.
My App.xaml
looks like this:
<Application x:Class="ThemingDemoApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ThemingDemoApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
<local:ThemeProvider x:Key="{x:Static local:ThemeProvider.DefaultInstance}" />
</ResourceDictionary>
</Application.Resources>
</Application>
I have tried adding a theme file to the App resources initially but this makes no difference.
Is it possible to use dynamic resources in globally defined styles, or do I have to implement dynamic styles on every control directly?
Here is a repo with a minimal example showing how it doesn't work: https://github.com/will-scc/ThemingDemoApp