According to MSDN, the standard way of setting your app theme is to set RequestedTheme="Dark"
or RequestedTheme="Light"
on the toplevel Application
instance.
This works great for simple apps but many times I find myself wanting to change the theme of an individual page or even an individual control (e.g. a light-themed text edit page vs a dark-themed image viewer in the same app).
XAML controls have 10s or 100s of visual states and color definitions and setting each one of them by hand is tedious and difficult to get 100% right!
Is there an easy way to set a dark or light theme on an individual control?
In folder Common you have a StandardStyles.xaml file.
Here you can find all standard styles default for Metro Applications. You need to uncomment styles what you want to use and add to control as StaticResource.
For a XAML windows store app, the default look-and-feel of controls is defined in
Common/StandardStyles.xaml
. If you've ever looked at that file, you'll notice a ton of references like{StaticResource ApplicationForegroundThemeBrush}
. Looks promising...Unfortunately, these "theme brushes" aren't defined anywhere in your app, and there's no easy way to set a light or dark override for individual controls. However, there is an answer.
Fortunately, there's an excellent blog post by Joe White on the default theme colors, which I've turned into a resource dictionary that you can find here. Dropbox only does xml previews so you'll have to rename the file.
Copying these resources to your app won't help by itself though. To make use of them, you need to surgically override the default control styles to use them!
One way to do this is to create a new resource dictionary, e.g. at
Common/CustomStyles.xaml
, and merge that into the app's resources like so:Notice that our default theme is
Light
. If we'd like to make aDark
-themedTextBlock
, let's copy the visual style fromStandardStyles.xaml
and add it to ourCustomStyles.xaml
and make a few changes.Notice that we have appended
Dark
to the style name and the theme brush definitions! You can do this via find and replace"ThemeBrush}"
=>"ThemeBrushDark}"
in your CustomStyles.xaml file.Now you can create a dark-themed text block like so:
Of course, you can use this technique for any other type of control as well. A little tedious, but the results are correct and it's not nearly as bad as trying to define every color manually!
There's no magic here. We're just defining some colors and overriding a default style with one that we copy-pasted and modified to use our colors.
I've also faced the same problem while Designing my App "Contact Book", because i also wanted the change between Dark & Light themes. For your que : "How do I mix Light and Dark themes in a C#/XAML Windows Store (Metro UI) App?", I have a superb solution.
Second solution:
pardon if you don't find your answer here, but i thought it might help you to dynamically change between "Dark" & "Light" themes setting in your win 8 metro app.
A solution that surprisingly does not seem to be mentioned is just to use RequestedTheme="Dark" or RequestedTheme="Light" on the individual control elements.
I do that for an app where I needed to set some appbarbuttons to the dark setting (which is white foreground) - because the Foreground property did not set both the circle and the glyph itself to white:
This way, I just force the control to use the theme I decide.