Apply Themes Dynamically

Dec 23, 2014 at 9:23 AM
I am using Fluent 2.2 version in my project, I want to apply themes dynamically in my project.
How can i achieve this. I also did some r&d for it and apply below code but not succeeded.

In App.xaml

<ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Generic.xaml" />

        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

In ThemeChange.cs page

Properties.Settings.Default.currentTheme = 1;
Properties.Settings.Default.ThemeChanged=true;
                    setTheme();
private void setTheme()
    {
        if (Properties.Settings.Default.ThemeChanged)
        {
            switch (Properties.Settings.Default.currentTheme)
            {
                case 0:
                    System.Windows.Application.Current.Resources.BeginInit();
                    System.Windows.Application.Current.Resources.MergedDictionaries.Insert(0, new ResourceDictionary() { Source = new Uri("pack://application:,,,/Fluent;component/Themes/Office2010/Blue.xaml") });
                    System.Windows.Application.Current.Resources.MergedDictionaries.RemoveAt(1);
                    System.Windows.Application.Current.Resources.EndInit();
                    Properties.Settings.Default.ThemeChanged = false;
                    break;
                case 1:
                    System.Windows.Application.Current.Resources.BeginInit();
                    System.Windows.Application.Current.Resources.MergedDictionaries.Insert(0, new ResourceDictionary() { Source = new Uri("pack://application:,,,/Fluent;component/Themes/Office2010/Silver.xaml") });
                    System.Windows.Application.Current.Resources.MergedDictionaries.RemoveAt(1);
                    System.Windows.Application.Current.Resources.EndInit();
                    Properties.Settings.Default.ThemeChanged = false;
                    break;
                case 2:
                    System.Windows.Application.Current.Resources.BeginInit();
                    System.Windows.Application.Current.Resources.MergedDictionaries.Insert(0, new ResourceDictionary() { Source = new Uri("pack://application:,,,/Fluent;component/Themes/Office2010/Black.xaml") });
                    System.Windows.Application.Current.Resources.MergedDictionaries.RemoveAt(1);
                    System.Windows.Application.Current.Resources.EndInit();
                    Properties.Settings.Default.ThemeChanged = false;
                    break;
            }
        }
    }
Developer
Jan 30, 2015 at 8:57 AM
Marked as answer by batzen on 1/30/2015 at 1:57 AM