views:

3502

answers:

4

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?

+4  A: 

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
+2  A: 

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
+2  A: 

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
If you don't specify a x:Key at all, the TargetType is automatically used as Key. DRY! (I love CornflowerBlue too!)
David Schmitt
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
bennage
+1  A: 

Does anyone know if the recently-released Composite Application Guidance from the MS patterns & practices group offers skinning abilities? http://www.codeplex.com/CompositeWPF

GregUzelac