Here is my solution
I created 4 resource files namely
Light.xaml, Dark.xaml, Black.xaml, Red.xaml. Each resource file contains the same named style setters for each element of the app with different values. I am using Style="{DynamicResource ResourceKey=name of style}" to assign the style in the XAML pages & windows.
Also in the App.config file create an appsettings key named AppTheme and assign it a default value.
In App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
string theme = ConfigurationManager.AppSettings["StashTheme"];
if (theme != null || theme != "")
{
AppThemeManager.ApplyTheme(new System.Uri("/Resources/" + theme + ".xaml", UriKind.Relative));
}
else
{
AppThemeManager.ApplyTheme(new System.Uri("Resources/Light.xaml", UriKind.Relative));
}
Dashboard dashboard = new dashboard();
dashboard.Show();
}
In AppThemeManager class
internal class AppThemeManager
{
public AppThemeManager() { }
public static void ApplyTheme(Uri themeURI)
{
ResourceDictionary theme_resx = new ResourceDictionary() { Source = themeURI };
App.Current.Resources.Clear();
App.Current.Resources.MergedDictionaries.Clear();
App.Current.Resources.MergedDictionaries.Add(theme_resx);
string[] parts = themeURI.ToString().Split('/');
string theme = parts.Last().Split('.')[0];
if (theme != "")
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["AppTheme"].Value = theme;
config.Save(ConfigurationSaveMode.Modified);
}
}
}
You can also call the ApplyTheme method on the button click or comboboxitem changed event.
Be sure to add any required resource files to the merged dictionary after clearing them out in the ApplyTheme method.