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!
Your problem is that you are trying to change the View directly from your ViewModel, which is not allowed. You need to come up with a more passive solution based on property bindings.
My approach would be have a small piece of code the your main view's code-behind that switches resource files in your merged dictionaries, and the way it does this can be disctated by the value of a property in your ViewModel that it is bound to. A small amount of code-behind to support View-centric behaviour is allowed in MVVM.
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
I handle Theme switching at start-up in my application like this.
I first clear the
Dictionaries
to remove any presetTheme
. I do this as I use a default theme in the editor, and then duringrun-time
switch depending on the users configuration.I restart the application to load the new theme, but as you save the states etc in your
ViewModel
you should be able to reload theUI
without having to completely restart the application. This was however not an requirement for my project, so I never went that far.You could probably just pass on the name of your theme from the
View
, and then parse it using logic from yourViewModel
.