Welcome to WindowsClient.net | Sign in | Join

Cristian Ricciolo Civera

Windows Presentation Foundation, C++ CLI, LINQ and experiments

Sponsors





  • advertise here

(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! :-)

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter