RSS Feed


Skinning a WPF Application

Yesterday, I wrote about the differences between theming and skinning in WPF. Today, I want to show how I plan to implement skinning a WPF application.

The general idea for skinning is to replace the application resource dictionary, which contains the styles, brushes, and templates, based on the user's input. In this case, the user can select a skin from a combo box. I will be using Petzold's excellent XAML Clock to demonstrate.

Here is a screenshot of my skinnable XAML Clock.

Here it is again with the Dark skin applied. I included four generic skins in this sample.

In order to change the clock hands and tick marks, I apply the stroke and fill properties to dynamic resources. Here's the XAML for the Hour hand. Note that the Fill and Stroke properties are set to dynamic resources.

<Path x:Name="HourHandPath" Data="M 0 15 L 10 0, 0 -60, -10 0 Z"

    Fill="{DynamicResource HourHandFillBrush}" Stroke="{DynamicResource ClockHandsBrush}">

</Path>

These resources are declared inside of a Resource Dictionary:

<ResourceDictionary

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<SolidColorBrush x:Key="MainWindowBackgroundBrush" Color="#FFFFFFFF"/>

<SolidColorBrush x:Key="ClockHandsBrush" Color="#FF000000"/>

<SolidColorBrush x:Key="TicksStrokeBrush" Color="#FF000000"/>

<SolidColorBrush x:Key="HourHandFillBrush" Color="#FFA0A0A0"/>

<SolidColorBrush x:Key="MinuteHandFillBrush" Color="#FFE3E3E3"/>

<SolidColorBrush x:Key="TextForegroundBrush" Color="#FF000000"/>

   

</ResourceDictionary>

The resource dictionary is then merged to be use by the application in App.xaml as follow:

<Application

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    x:Class="WPFSkinning.App"

    StartupUri="Window1.xaml">

    <Application.Resources>

        <!-- Resources scoped at the Application level should be defined here. -->

        <ResourceDictionary>

            <ResourceDictionary.MergedDictionaries>

                <ResourceDictionary Source="Skins\Default\DefaultResources.xaml"/>

            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>

    </Application.Resources>

</Application>

I organized each of the skin resources into their own resource dictionaries in separate folders.

To change the skin based on the combo box, I have an event handler for the selection changed event. The skins are loaded with Application.LoadComponent which makes them objects. The application resources are then changed to the new resource dictionary for the corresponding skin.

private void SkinsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

ResourceDictionary rd;

switch ((Skin)((ComboBox)sender).SelectedItem)

{

case Skin.Dark:

rd = Application.LoadComponent(new Uri(@"Skins/Dark/DarkResources.xaml", UriKind.Relative)) as ResourceDictionary;

break;

case Skin.Triangle:

rd = Application.LoadComponent(new Uri(@"Skins/Triangle/TriangleResources.xaml", UriKind.Relative)) as ResourceDictionary;

break;

case Skin.Blue:

rd = Application.LoadComponent(new Uri(@"Skins/Blue/BlueResources.xaml", UriKind.Relative)) as ResourceDictionary;

break;

default:

rd = Application.LoadComponent(new Uri(@"Skins/Default/DefaultResources.xaml", UriKind.Relative)) as ResourceDictionary;

break;

}

Application.Current.Resources = rd;

}

You can try the XBAP here: WPF Skinnable Clock XBAP

You can download the source here: WPF Skinning

 
Posted by Alan Le | 10 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Friday, 20 Jul 2007 06:18 by Re: Skinning a WPF Application
<div class=ExternalClass6D25EF420ECC48469ECDAEAD9D9D766F><div>To me, when you said skinning the application is the regular rectangular Windows will be replaced with your skin.   In this case it should be the Clock that can be dragged as a regular Windows.</div> <div> </div> <div> </div> <div>But thanks for your work anyway</div></div>

Monday, 30 Jul 2007 12:49 by Re: Skinning a WPF Application
<div class=ExternalClass8526A44911C04312867B5AFD0B321460><div>It was very helpfull. Keep it up man.</div> <div>Thanks</div></div>

Friday, 7 Dec 2007 02:33 by Marketplace for Skins
<div class=ExternalClass1ECF24C80103470AA2784DCACFE8E047><div>Like most developers, I am not very talented at creating nice looking UI. Do you know of a marketplace for skins that we can use to give our application a nice look &amp; feel?</div> <div> </div> <div>Philipp</div></div>

Friday, 7 Dec 2007 03:42 by Re: Skinning a WPF Application
<div class=ExternalClassC9ED13780DE6484E9C34CC858A33D699>Hi Philipp, At Vertigo, we do our visual ui, interactive design, and ui implementation all in house for our apps and our clients apps. I understand what you mean; however, I'm not aware of any particular sites or places that will sell pre-skinned controls for WPF though. Maybe once WPF applications are more standard, resources around WPF UI will start popping up. </div>

Thursday, 10 Jan 2008 10:59 by Re: Skinning a WPF Application
<div class=ExternalClass7F93E1468E574C40BF05C0576756C0D9>Wow, I've been reading a lot today about skinning WPF and this post really stands out. You just explained in a few screens what others took huge amounts to cover. Outstanding work! Your demo app is especially well crafted to convey what's going on because its so bloody simple! Cheers, Matthew</div>

Friday, 11 Jan 2008 03:11 by Re: Skinning a WPF Application
<div class=ExternalClassF0B421AB91644FECBBA05384B93CF048>Glad you like it Matthew! You can see this in practice on the Witty application. http://code.google.com/p/wittytwitter/</div>

Tuesday, 29 Jan 2008 04:57 by Re: Skinning a WPF Application
<div class=ExternalClass9C2263C706D6432B8D90B41B82B21999><div>Hi Alan,</div> <div> </div> <div>Is this sort of technique possible with external XAML files? I'm thinking along the lines of allowing a 3rd party to ship a XAML file full of DataTemplates or Styles, and loading it (out of a known folder) to override the ones built into your application.</div> <div> </div> <div>Cheers,</div> <div>Matt</div></div>

Tuesday, 29 Jan 2008 06:20 by Re: Skinning a WPF Application
<div class=ExternalClass1FB8BCE24A384916B5389290327F46AB><p>Alan,</p> <p>Got it! By defining the &quot;default&quot; templates for my types in App.Xaml, I was able to &quot;swap them out&quot; for custom ones in a loose Xaml file like this:</p><pre><font size=2>Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); ofd.DefaultExt = &quot;.xaml&quot;; ofd.Filter = &quot;XAML Files|*.xaml&quot;; if (ofd.ShowDialog() == true) { ResourceDictionary rd = XamlReader.Load(ofd.OpenFile()) as ResourceDictionary; if (rd != null) { foreach (var key in rd.Keys) { Application.Current.Resources[key] = rd[key]; } } } </font></pre></div>

Monday, 18 Feb 2008 08:30 by Question about the default skin
<div class=ExternalClass91E340B83A864AA696EFB90457A28467><div>How/where does the default skin get loaded ?</div> <div> </div> <div>In the file &quot;App.xml&quot; I have tried changing the line<font color="#a31515" size=2> <p></font><font color="#0000ff" size=2>&lt;</font><font color="#a31515" size=2>ResourceDictionary</font><font color="#ff0000" size=2> Source</font><font color="#0000ff" size=2>=&quot;Skins\Default\DefaultResources.xaml&quot;/&gt;</font></p> <p><font color="#0000ff" size=2>to <font color="#a31515" size=2></p> <p></font><font color="#0000ff" size=2>&lt;</font><font color="#a31515" size=2>ResourceDictionary</font><font color="#ff0000" size=2> Source</font><font color="#0000ff" size=2>=&quot;Skins\Blue\BlueResources.xaml&quot;/&gt;</font></p> <p><font color="#0000ff" size=2>but that didn't load the Blue skin on startup</font></p> <p><font color="#0000ff" size=2> </p></font></font></div></div>

Monday, 18 Feb 2008 12:10 by Question
<div class=ExternalClassF9356E5068BA47ABB00AD248A2B3C3A3><div> <div><font face=Arial size=2>I'm trying to understand how the skins get loaded when the application is first loading, if I change the order of the enum, that that skin loads with the application first starts.  Can you please explain why that is ?</font></div></div></div>

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation