0

I am new to WPF MVVM and I need to develop a new application (which will replace an old app written in WinForms with SQL Server database). I would like to know what would be the best way to change styles programatically - e.g. from a default style to a 'dark theme'.

I am not sure if my approach is correct but for now I've setup a 'default style' which consists of two XAML files which I reference in the App.xaml as follows:

<Application x:Class="MyApp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:MyApp"
         StartupUri="/AppGeneral/MainWindowView.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

            <!-- Default style -->
            <ResourceDictionary Source="/Framework/Resources/Styles/Default/AppStyle.xaml"/>
            <ResourceDictionary Source="/Framework/Resources/Styles/Default/CommonStyle.xaml"/>

        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

The first file defines styles for the overall look of the application, and the second file contains common styles applied too all views. This simple implementation works for me so far, but I would like to add another style, such as a 'DarkTheme' for example:

<!-- Dark theme -->
<ResourceDictionary Source="/Framework/Resources/Styles/DarkTheme/AppStyle.xaml"/>
<ResourceDictionary Source="/Framework/Resources/Styles/DarkTheme/CommonStyle.xaml"/>

And I am not sure how to go about doing it so that I can change in between the styles programatically (i.e. allow the user to change the style). Any help is greatly appreciated.

user2430797
  • 320
  • 5
  • 18
  • 2
    You can find various examples of theming on SO and google. Search 'Theming WPF application' – Insane Dec 31 '19 at 03:09
  • 1
    Does this answer your question? [Change theme at runtime](https://stackoverflow.com/questions/6229724/change-theme-at-runtime) – Pavel Anikhouski Dec 31 '19 at 13:34
  • That's good. Thanks Pavel! – user2430797 Jan 02 '20 at 02:14
  • I tried what's suggested on that thread (see last post) and it doesn't change my theme. It seems that it does not apply it to my view. Do you know what could be wrong? Thanks. – user2430797 Jan 02 '20 at 07:24
  • I can see that it updates other views that I open after I changed the theme, but it does not update the currently open view (MainWindow). Is there a way to refresh the open view? – user2430797 Jan 02 '20 at 07:28
  • Eventually I found the answer: use DynamicResource instead of StaticResource. – user2430797 Jan 02 '20 at 07:45

0 Answers0