MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.32k stars 2.44k forks source link

Accent Brush does not change when theme is changed from code #4163

Closed TripleNico closed 3 years ago

TripleNico commented 3 years ago

I bumped into an issue and maybe i'm doing it wrong but this is what i experience:

Our application uses the Dark.Blue Theme which is declared in Application.xaml for example:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Dark.Blue.xaml" />   
</ResourceDictionary.MergedDictionaries>

In the MainWindow.xaml we have set the GlowBrush like this:

GlowBrush="{StaticResource MahApps.Brushes.Accent}"

More complete:

<mah:MetroWindow
    x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
    xmlns:local="clr-namespace:MyApp"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:materialDesignConverters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Width="1280"
    Height="800"
    MinWidth="1280"
    MinHeight="800"
    AllowsTransparency="True"
    Background="{StaticResource MahApps.Brushes.ThemeBackground}"
    GlowBrush="{StaticResource MahApps.Brushes.Accent}"
    Language="en-EN"
    NonActiveGlowBrush="{StaticResource MahApps.Brushes.Control.Validation}"
    Opacity="0"
    ResizeMode="CanResizeWithGrip"
    ShowCloseButton="False"
    TitleCharacterCasing="Normal"
    WindowStartupLocation="CenterScreen"
    WindowState="Normal"
    mc:Ignorable="d">

              ........ All UI Stuf ........

</mah:MetroWindow>

Now if upon startup a certain condition is met then we change the theme from code, using this:

' Set the application theme to Dark.Green
ThemeManager.Current.ChangeTheme(Me, "Dark.Green")

As described here: Theme Usage

Now this does change the UI to Dark.Green except the GlowBrush stays Blue. This is either because the update of the property isn't notified or the ThemeManager doesn't change the MahApps.Brushes.Accent ? If i change the Application.xaml to use Dark.Green then the MahApps.Brushes.Accent is green.

MahApps.Metro version: v2.4.7
Windows build number: Win10 pro 20H2 [Version 10.0.19042.685]
Visual Studio: 2019 16.11.0
Target Framework: .Net Framework v4.7.2
TripleNico commented 3 years ago

Doh! I overlooked that GlowBrush has a StaticResource assigned. That's why it didn't change.... For more explanation https://stackoverflow.com/a/200875/4847008

timunie commented 3 years ago

@TripleNico thank you for sharing your own solution 👍