0

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

WSC
  • 903
  • 10
  • 30

1 Answers1

1

The global window style in Controls.xaml isn't applied to the window in your example. This is not related to the theme manager you are using.

You should explicitly set the Style property of the window or use any of the other approaches suggested here to apply the Style to the Window:

<Window x:Class="ThemingDemoApp.MainWindow"
        ...
        Style="{StaticResource windowStyle}" />

Control.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="windowStyle" TargetType="{x:Type Window}">
        <Setter Property="Background"
                Value="{DynamicResource WhiteBrush20}" />
    </Style>

</ResourceDictionary>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Ah, I see your point. If I set a global style for `x:Type Button` the colour is correctly applied as I would expect. So is my understanding correct that `x:Type Window` doesn't work because my window is actually type `MainWindow` which, although it inherits `Window`, styles don't apply to inherited classes? – WSC Jun 14 '23 at 14:24
  • 1
    Something like that. See the link I provided for more information about the styling of windows. – mm8 Jun 14 '23 at 14:29