My project uses the ProjectTheme.xaml file for all WPF windows through the project. The ProjectTheme.xaml file references the style theme as follows
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/MyProject;component/Themes/WPFThemes/Customized.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
All links to WPF Windows WindowBase.xaml
<Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/MyProject;component/View/WindowBase.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources>
WindowBase.xaml refers to the custom bar1.xaml header
<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/MyProject;component/Themes/WPFThemes/Bar1.xaml" /> </ResourceDictionary.MergedDictionaries>
Links Bar1.xaml ProjectTheme.xaml
<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/MyProject;component/ProjectTheme.xaml"/> </ResourceDictionary.MergedDictionaries>
So the heriarchy
- Links Window1 WindowBase.xaml
- Links WindowBase Bar1.xaml
- Links Bar1 ProjectTheme.xaml
- ProjectTheme.xaml refers to the actual theme resource file.
It works great. Now I want to dynamically change the theme of the project at runtime without leaving the application. Assuming I have some theme style files
- Customized.xaml
- Customized1.xaml
- Customized2.xaml
My question is: if it is possible to dynamically update the ProjectTheme.xaml file at run time to change the line from
<ResourceDictionary Source="/MyProject;component/Themes/WPFThemes/Customized.xaml" />
to
<ResourceDictionary Source="/MyProject;component/Themes/WPFThemes/Customized1.xaml" />
to achieve my goal? If so, how do I do this? If not, what is the reason and what is the best (other) way to achieve my goal?
I tried the following, but none of them work: the style does not change.
method 1
Application.Current.Resources.MergedDictionaries.Clear(); Uri NewTheme = new Uri(@"/MyProject;component/Themes/WPFThemes/Customized2.xaml", UriKind.Relative); ResourceDictionary dictionary = (ResourceDictionary)Application.LoadComponent(NewTheme); Application.Current.Resources.MergedDictionaries.Add(dictionary);
way 2
Application.Current.Resources.MergedDictionaries.RemoveAt(0); Uri NewTheme = new Uri(@"/MyProject;component/Themes/WPFThemes/Customized2.xaml", UriKind.Relative); ResourceDictionary dictionary = (ResourceDictionary)Application.LoadComponent(NewTheme); Application.Current.Resources.MergedDictionaries.Insert(0, dictionary);
Note: In my real theme-style files (Customized.xaml ...), I used a combination of a dynamic resource and a static resource. It is important?
Thanks in advance.