Forcing WPF to use a specific theme

3 10 2009

How to force WPF to use Windows default themes, such as Classic, Luna and Aero themes…

Requirements

Before you begin there are a few things that you need.

Just Beginning…

There are a few ways to do this, but the easiest is the first way I am going to show you.

Firstly, if you do not have a project to test it on, I would recommend you make one and try it as you read this, as it makes things a whole lot easier for you.

So now you have  a blank WPF project, you can start by dragging some random controls onto the area sch as Buttons, Listboxes, Textboxes etc.

Random Controls

Random Controls

As you can see I am using Windows Vista, but you can do this on XP too.

The Coding…

Next, on the Solution Explorer, double click on “Application.xaml”, this is where it will all take place. You should now be in the code editor and see this code in there.

Application.xaml

Application.xaml

In between the Application.Resources tags, add this to enable the Windows Classic theme:

<ResourceDictionary Source="/PresentationFramework.Classic, Version=3.0.0.0,
 Culture=neutral, PublicKeyToken=31bf3856ad364e35,
 ProcessorArchitecture=MSIL;component/themes/classic.xaml" />

So it should all together look something like this:

<Application x:Class="Application"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 StartupUri="Window1.xaml">
 <Application.Resources>
 <ResourceDictionary Source="/PresentationFramework.Classic, Version=3.0.0.0,
 Culture=neutral, PublicKeyToken=31bf3856ad364e35,
 ProcessorArchitecture=MSIL;component/themes/classic.xaml" />
 </Application.Resources>
</Application>

Once the code is in there, hit F5 and run the program. You will see that all of your default button and control have had their styles changed to the Windows Classic theme.

The Windows Classic style applied to user controls.

The Windows Classic style applied to user controls.

To add Vista’s Aero theme, replace the Windows Classic ResourceDictionary with this:

<ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0,
 Culture=neutral, PublicKeyToken=31bf3856ad364e35,
 ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />

And you will notice that it has Vista’s Aero theme, even if you are on XP. And finally,to add XP’s Luna theme to the user controls, replace the ResourceDictionary with this:

<ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0,
 Culture=neutral, PublicKeyToken=31bf3856ad364e35,
 ProcessorArchitecture=MSIL;component/themes/luna.normalcolor.xaml" />
XP's Luna theme.

XP's Luna theme.

Some Others…

There are colour variations of Luna, they are listed below, along with the code.

  • Metallic (Silver)
<ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0,
 Culture=neutral, PublicKeyToken=31bf3856ad364e35,
 ProcessorArchitecture=MSIL;component/themes/luna.metallic.xaml" />
  • Homestead (Green)
<ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0,
 Culture=neutral, PublicKeyToken=31bf3856ad364e35,
 ProcessorArchitecture=MSIL;component/themes/luna.homestead.xaml" />

And the code for XP’s Royale theme.

<ResourceDictionary Source="/PresentationFramework.Royale, Version=3.0.0.0,
 Culture=neutral, PublicKeyToken=31bf3856ad364e35,
 ProcessorArchitecture=MSIL;component/themes/royale.normalcolor.xaml" />

And that’s all for now. I hope you enjoyed this post.


Actions

Information

One response

3 10 2009
Cyclone103

Very cool!

Leave a comment