I’m working on a WPF project using MVVM and I’m trying to implement a feature that changes the theme dynamically. The theming info is located in separate xaml files (ie Theme1.xaml, Theme2.xaml). I want to do the actual theme changing in the ViewModel class rather than in the code behind file of View.xaml for various reasons.
I’ve tried a couple ideas but can’t get anything to work:
-
I tried binding the ResourceDictionary of View to a variable in ViewModel but am told that a binding cannot be set on the Source property of type ResourceDictionary
-
I don’t have any sort of View object in my ViewModel class on which to call a “UpdateTheme” method
Any ideas on how I can change the MergedDictionary reference in my View class from the ViewModel class?
Thanks!
I have worked with the same time problem earlier here what i did in my case may be it can help you out.
Copy all you theme files (theme1.xaml, theme2.xaml…) into Themes folder at you exe path. and try with below sample code. using Bindings
C#:
XAML: