ChangeAppStyle only works once
I try to implement a Light/Dark-Switch. When i press the button, ChangeAppStyle works once. When i press it again, nothing happens. No error, no change (only the background). It doesn't matter, which Theme i choose ... if i use BaseDark in App.xaml, i can change once to BaseLight, but not back to BaseDark.
private void lightDARK_Click(object sender, RoutedEventArgs e)
{
try
{
MainWindow mw = (MainWindow)Application.Current.MainWindow;
SolidColorBrush color;
if (mw.Background.ToString() == "#FF696969")
{
ThemeManager.ChangeAppStyle(this, ThemeManager.GetAccent("Blue"), ThemeManager.GetAppTheme("BaseLight"));
color = Brushes.LightGray;
}
else
{
ThemeManager.ChangeAppStyle(this, ThemeManager.GetAccent("Blue"), ThemeManager.GetAppTheme("BaseDark"));
color = Brushes.DimGray;
}
mw.Background = color;
}
catch (Exception ex)
{
throw new Exception("Error: " + ex.Message);
}
}
@DerKiLLa Are you really have all necessary resources in your App.xaml? Like
<Application x:Class="WpfApp4.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp4"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

Your sample shows exactly my behaviour. At first, u have a bright button (Theme: BaseLight), then u click several times the button and the button stays dark (Theme: BaseDark). Only the background changes.
app.xaml:
<Application x:Class="zTiredV2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:zTiredV2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
@DerKiLLa Ah, sorry, my mistake. You have 2 options to fix this.
- use the current Application to change the theme, because you initialize it at the App.xaml
ThemeManager.ChangeAppStyle(Application.Current, .... );
- add the accent and theme resource to the Window resources when you use the ChangeSppStyle method with this (the Window), because the Window need also the accent and theme initialization then.
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

Thanks very much! Application.Current works perfectly! :)