How do you apply WPF Fluent theme (+ Application.ThemeMode theme-aware) to a control's Background?

3 weeks ago 12
ARTICLE AD BOX

.NET 9.0 introduced Fluent theme support out of the box for WPF applications. I'm using it in my applications, but I've got a question: how do you set the background of a control, say Border, to reflect the default window fluent theme background, and have it react to ThemeMode changes?

Note that by default, fluent theme already applies both backgrounds and theme awareness to most controls (MenuItem, Grid, Button, TextBlock, ListView, you name it), but my control doesn't seem to have a background (it's transparent) and I would like to apply the WPF fluent theme styling to it via XAML (or C# code-behind if XAML is not feasible), just like it does by default to controls like Grid or Window backgrounds.

To elaborate: the new fluent theme supports what's known as Application.ThemeMode and Window.ThemeMode. Though experimental, it's currently the simplest way to switch between Light/Dark Fluent mode themes. When Application.ThemeMode is set to, say, Dark mode, the entire application's UI animates from Light mode to Dark mode. I would like my control to not only reflect the Fluent theme's color but also react to theme mode changes, that is, when Application.ThemeMode is modified, ideally via XAML. I'm not really sure how to accomplish this, so any help would be appreciated.

I did also try to use Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" but it just sets the background to white, and does not react to Dark mode changes.

Read Entire Article