Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a WPF program match the currently selected Windows Theme

Tags:

c#

.net

wpf

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")

like image 959
ford Avatar asked Jan 14 '12 00:01

ford


2 Answers

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.

like image 145
Carlo Avatar answered Oct 12 '22 23:10

Carlo


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.

like image 43
Maxim V. Pavlov Avatar answered Oct 12 '22 23:10

Maxim V. Pavlov