(Real) Force theme on WPF

When I develop custom control for WPF that supports theming I need to force the engine to load the right resource instead of my Windows Vista theme.  There are some tricks on the web that does is merging the the resources for a specific theme but this works only for default WPF controls.

Here you are a snippet to force a specific theme. It uses reflection so use it only for development purpose:

public App() 
{ 
   
//ForceTheme("Luna", "NormalColor"); 
    //ForceTheme("Luna", "Homestead"); 
    //ForceTheme("Luna", "Metallic"); 
    //ForceTheme("Aero", "NormalColor"); 
    //ForceTheme("Generic", ""); 
} 

private static void ForceTheme(string themeName, string themeColor) 
{ 
   
// To force a particular style 
    Type t = Type.GetType("MS.Win32.UxThemeWrapper, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); 
   
FieldInfo themeNameField = t.GetField("_themeName", BindingFlags.NonPublic | BindingFlags.Static); 
   
themeNameField.SetValue(null, themeName); 
   
FieldInfo themeColorField = t.GetField("_themeColor", BindingFlags.NonPublic | BindingFlags.Static); 
   
themeColorField.SetValue(null, themeColor); 
}
 
Enjoy! :-)
Published 07 November 2008 04:01 PM by RiccioloCristian
Filed under: ,

Comments

# Mukesh said on 19 May, 2009 03:25 AM
Ultimate Solution, rest everywhere the solution looks hardcoded kind of. like setting in resource dictionary. Thanks a lot

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Search

Go

This Blog

Sponsors





Syndication

 
Page view counter