24

I want my WPF application to be skinnable, by applying a certain XAML template, and the changes to be application wide, even for dynamic controls or controls that aren't even in the visual/logical tree.

What can I use to accomplish this type of functionality? Are there any good resources or tutorials that show how this specific task can be done?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pop Catalin
  • 61,751
  • 23
  • 87
  • 115

3 Answers3

10

The basic approach to take is using resources all through your application and dynamically replacing the resources at runtime.

See http://www.nablasoft.com/alkampfer/index.php/2008/05/22/simple-skinnable-and-theme-management-in-wpf-user-interface/ for the basic approach

Maurice
  • 27,582
  • 5
  • 49
  • 62
  • 1
    Hi Maurice, both these links are now 404's. Any chance of finding some replacements? – Matt Jun 23 '15 at 20:12
  • @Matt https://web.archive.org/web/20130411050848/http://www.codewrecks.com/blog/index.php/2008/05/22/simple-skinnable-and-theme-management-in-wpf-user-interface – Xcodo Jul 23 '15 at 10:44
4

The replacing of resource will work but I found "structural skinning" to be more powerfull! Read more about it on CodeProject...

http://www.codeproject.com/KB/WPF/podder1.aspx

rudigrobler
  • 17,045
  • 12
  • 60
  • 74
  • An old question and answer but, unless I'm mistaken, it seems that the method alluded to on that page also relies (at least in part) on dynamic resource referencing. – Mike G Nov 02 '12 at 12:51
2

I have found the way to apply generic templates to all controls without using template keys. The solution is to use the type of the control as the Style key.

Example:

 <Application.Resources>
    <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
        <Setter Property="Button.Background" Value="CornflowerBlue"/>
        <Setter Property="Button.Template">
            <Setter.Value>
                <ControlTemplate x:Name="MyTemplate">
                    ...
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

here the Style key is x:Key="{x:Type Button}", so the style will be applied to all controls of type button without the control declaring the Style property to be a static or dynamic resource.

Pop Catalin
  • 61,751
  • 23
  • 87
  • 115
  • 4
    If you don't specify a x:Key at all, the TargetType is automatically used as Key. DRY! (I love CornflowerBlue too!) – David Schmitt Oct 17 '08 at 13:12
  • 2
    Also, some odd bits and pieces have built in keys that you just have to know about. For example, the separators in menus. http://devlicious.com/blogs/christopher_bennage/archive/2008/06/19/styling-separators-in-wpf.aspx – Christopher Bennage Oct 30 '08 at 13:01