9

I would like to make my WPF application draw from the currently selected system theme.

To illustrate, here is a Windows Forms version of what I hope to accomplish.

A basic winforms window with toolstrip and menustrip

This Windows Form window has a basic menustrip and a toolstrip with to specific theming. Its appearance will change if the user opts to change the theme:

same window, in green

Additionally, it will switch to areo theme when run in Windows 7. I would like to do the same in WPF. But I am unsure about how to make a WPF window styled after the current windows theme. This window (xaml), also has a menustrip and toolstrip control, but does not fully follow the selected Windows theme.

WPF needs something in order to theme correctly

The menu and toolbar controls do not retain the gradient found in the XP Luna themes. Note that unlike other WPF theming posts, I am not trying to override the User Selected Windows theme. This question seems close to mine, but I want to theme all controls to match the selected system theme unless overridden. Does this need to be done on a per control basis? Can this be configured for the entire project? This MSDN article only covers custom controls.

I would appreciate specific examples about how to get the WPF Menu and WPF toolbar controls to match the current system theme.

Edit: It would also be great to know what color to set the menu to such that a system themed stripe appears next to the menu item: (to the left of "Exit")

screenshot of open menu

(to the left of "Exit")

Community
  • 1
  • 1
ford
  • 1,839
  • 3
  • 20
  • 38

2 Answers2

6

One way is to use system colors like these. They will change based on the theme accordingly. I'm not sure if there's an easier way, but this is a good one =). There's a list below that shows you the colors per theme.

Carlo
  • 25,602
  • 32
  • 128
  • 176
  • That's a great link! Unfortunately I'm still a bit stumped: which color should I set the Background property of the Menu control in order to have it take on a themed gradient when the system is working /w Luna theme (1st and 2nd screenshots) – ford Jan 16 '12 at 17:57
  • @ford I'm not sure... the color names should be self descriptive. I know the names are not as useful as they could, but at least it narrows down their usage :) For example there are 3 that start with "MenuSomething" in the middle of the list. Those could be the ones you're looking for. – Carlo Jan 17 '12 at 23:18
  • You can also use this program to test which color is which by changing it from the windows control panel. This is the program http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-38-64-SystemColors+Reference/3482.SystemColorsSwatch.zip – Carlo Jan 17 '12 at 23:21
  • And here's how you test it http://blogs.msdn.com/b/wpf/archive/2010/11/30/systemcolors-reference.aspx#PT3 – Carlo Jan 17 '12 at 23:22
  • The gist of my question being this: I don't need a specific color, but I do need the colors to change when the user swiches themes (windows classic vs areo vs luna) – ford Jan 19 '12 at 21:35
  • 1
    Yes. Those system colors will change when the user switches theme. Did you try this? http://blogs.msdn.com/b/wpf/archive/2010/11/30/systemcolors-reference.aspx#PT3 – Carlo Jan 19 '12 at 22:18
  • Excellent! This method is working for changing some of the screen colors to match current theme (the brushes must be registered as dynamic resources on top of using the correct System.Color) – ford Feb 02 '12 at 00:42
2

The problem is that WPF controls are internally very different from Windows Forms one's or any other GDI elements. WPF control's look is defined with the help of a different classes in .Net. The same themeing can't just magically be applied to them.

Therefore I would say - yes, you should implement themeing on a per-control basis using the resource files in this question's answer.

I assume we are lucky MS has provided us with those at all. Another thing is that the very existence of those xaml styles proves how much more configurable the WPF framework is.

The theme xaml files are in {Program Files}\Microsoft Expression\Blend 4\SystemThemes\Wpf\

The question that you have initially referenced has an example of how to link a them in it's answer: Windows 7 theme for WPF?

To swap the theme you can dynamically add/remove entry from Application.Current.Resources.MergedDictionaries

You will have to detect the current theme yourself.

Community
  • 1
  • 1
Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174
  • I agree with the SO question that you linked to, but I asked this one primarily because I couldn't quite get what I wanted with the solution posted there. Do those xaml files (say, Luna.xaml for example) need to be loaded from somewhere? An example of this working on a menu bar or toolbox bar would be great. – ford Jan 20 '12 at 00:20
  • 1
    Ford, I have updated an answer a bit. I know you have seen both questions before you even wrote a question, but from what I know, there is just no easier and more automatic approach to working around your problem. – Maxim V. Pavlov Jan 20 '12 at 00:38
  • Maxim, thanks for your helpful response. Especially noteworthy is the part where I need to detect the theme myself. Nowhere else specifically mentioned that. – ford Jan 20 '12 at 16:38