At runtime, I want to change my app's main theme (which in this case is the light theme) to the dark theme.
I'm using the C# programming language, with the WPF project template. And in this project.
I installed the MaterialDesignThemes library in my project, and by default I set it to the light theme.
<materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="DeepOrange" />
Now I want to create a mechanism that can change the theme from light to dark at application runtime.
We can divide this question into 2 occasions:
Using only Code-Behind
xaml:
code-behind:
Using MVVM architecture
xaml:
ViewModel:
Note
You may have noticed that the MainViewModel is being inherited by the class called
ViewModelBase, and also in the constructor of the class, an instance was created with theViewModelCommandclass.For you to understand what these classes are, you need to know how to implement the MVVM architecture in your project.
In general it can be said that:
void OnPropertyChange(string property);which is responsible for notifying each change that is made to a property in the front-end (View).RelayCommandis the class that handles each event in a generic way. When it comes to an event, in the remaining ViewModels, a ReadOnly property of typeICommandis used to serve as a trigger in View controls, as in the case of<ToggleButton/>.