简体   繁体   中英

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

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.

一个带有工具条和menustrip的基本winforms窗口

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:

同一个窗口,绿色

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需要一些东西才能正确主题

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

打开菜单的屏幕截图

(to the left of "Exit")

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM