customizing-controltemplate

WPF ControlTemplate Patterns

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "customizing-controltemplate" with this command: npx skills add christian289/dotnet-with-claudecode/christian289-dotnet-with-claudecode-customizing-controltemplate

WPF ControlTemplate Patterns

All controls inherited from the Control class can completely redefine their visual structure through ControlTemplate.

  1. Core Concepts

ControlTemplate vs Style

Aspect Style ControlTemplate

Role Batch property value setting Visual structure redefinition

Scope Property changes only Full appearance change possible

Target All FrameworkElements Control-derived classes only

ControlTemplate Components

  • TargetType: Control type to which the template applies

  • TemplateBinding: Connection to TemplatedParent properties

  • ContentPresenter: Specifies where Content property is rendered

  • Triggers: State-based visual changes

  1. Basic Implementation Patterns

2.1 Button ControlTemplate (XAML)

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

&#x3C;Style TargetType="{x:Type Button}" x:Key="RoundedButtonStyle">
    &#x3C;Setter Property="Template">
        &#x3C;Setter.Value>
            &#x3C;ControlTemplate TargetType="{x:Type Button}">
                &#x3C;!-- Visual structure definition -->
                &#x3C;Border x:Name="PART_Border"
                        CornerRadius="10"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}">

                    &#x3C;!-- Content rendering location -->
                    &#x3C;ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      RecognizesAccessKey="True"/>
                &#x3C;/Border>

                &#x3C;!-- State-based triggers -->
                &#x3C;ControlTemplate.Triggers>
                    &#x3C;Trigger Property="IsMouseOver" Value="True">
                        &#x3C;Setter TargetName="PART_Border" Property="Background" Value="#E3F2FD"/>
                    &#x3C;/Trigger>
                    &#x3C;Trigger Property="IsPressed" Value="True">
                        &#x3C;Setter TargetName="PART_Border" Property="Background" Value="#BBDEFB"/>
                    &#x3C;/Trigger>
                    &#x3C;Trigger Property="IsEnabled" Value="False">
                        &#x3C;Setter TargetName="PART_Border" Property="Opacity" Value="0.5"/>
                    &#x3C;/Trigger>
                &#x3C;/ControlTemplate.Triggers>
            &#x3C;/ControlTemplate>
        &#x3C;/Setter.Value>
    &#x3C;/Setter>

    &#x3C;!-- Default property values -->
    &#x3C;Setter Property="Background" Value="#2196F3"/>
    &#x3C;Setter Property="Foreground" Value="White"/>
    &#x3C;Setter Property="BorderThickness" Value="0"/>
    &#x3C;Setter Property="Padding" Value="16,8"/>
    &#x3C;Setter Property="Cursor" Value="Hand"/>
&#x3C;/Style>

</ResourceDictionary>

2.2 Applying ControlTemplate in Code

namespace MyApp.Helpers;

using System.IO; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Markup;

public static class TemplateHelper { /// <summary> /// Create ControlTemplate from XAML string /// </summary> public static ControlTemplate CreateTemplate(string xaml) { using var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml));

    var context = new ParserContext();
    context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
    context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");

    return (ControlTemplate)XamlReader.Load(stream, context);
}

}

  1. TemplateBinding vs Binding

3.1 TemplateBinding (Recommended)

<!-- Compile-time binding, one-way, better performance --> <Border Background="{TemplateBinding Background}"/>

3.2 RelativeSource TemplatedParent (When Two-Way Needed)

<!-- Runtime binding, two-way possible, relatively slower --> <Border Background="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}"/>

Comparison

Aspect TemplateBinding RelativeSource TemplatedParent

Direction One-way (OneWay) Two-way possible

Performance Fast Relatively slower

Converter Not available Available

Use case Most cases When two-way/converter needed

  1. ContentPresenter Details

4.1 Key Properties

<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"/>

4.2 RecognizesAccessKey

<!-- Enable button activation with Alt + underlined character --> <Button Content="_Save"/> <!-- Activate with Alt+S -->

  1. Trigger Patterns

5.1 Property Trigger

<ControlTemplate.Triggers> <!-- Single property condition --> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="PART_Border" Property="Background" Value="LightBlue"/> </Trigger> </ControlTemplate.Triggers>

5.2 MultiTrigger

<ControlTemplate.Triggers> <!-- Multiple property conditions (AND) --> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True"/> <Condition Property="IsEnabled" Value="True"/> </MultiTrigger.Conditions> <Setter TargetName="PART_Border" Property="Background" Value="LightGreen"/> </MultiTrigger> </ControlTemplate.Triggers>

5.3 EventTrigger (Animation)

<ControlTemplate.Triggers> <EventTrigger RoutedEvent="MouseEnter"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="PART_Border" Storyboard.TargetProperty="Opacity" To="0.8" Duration="0:0:0.2"/> </Storyboard> </BeginStoryboard> </EventTrigger> </ControlTemplate.Triggers>

  1. Practical Example: Toggle Button

<Style TargetType="{x:Type ToggleButton}" x:Key="SwitchToggleStyle"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Grid> <!-- Background track --> <Border x:Name="PART_Track" Width="50" Height="26" CornerRadius="13" Background="#E0E0E0"/>

                &#x3C;!-- Sliding thumb -->
                &#x3C;Border x:Name="PART_Thumb"
                        Width="22" Height="22"
                        CornerRadius="11"
                        Background="White"
                        HorizontalAlignment="Left"
                        Margin="2,0,0,0">
                    &#x3C;Border.Effect>
                        &#x3C;DropShadowEffect ShadowDepth="1" BlurRadius="3" Opacity="0.3"/>
                    &#x3C;/Border.Effect>
                &#x3C;/Border>
            &#x3C;/Grid>

            &#x3C;ControlTemplate.Triggers>
                &#x3C;!-- Checked state -->
                &#x3C;Trigger Property="IsChecked" Value="True">
                    &#x3C;Setter TargetName="PART_Track" Property="Background" Value="#4CAF50"/>
                    &#x3C;Setter TargetName="PART_Thumb" Property="HorizontalAlignment" Value="Right"/>
                    &#x3C;Setter TargetName="PART_Thumb" Property="Margin" Value="0,0,2,0"/>
                &#x3C;/Trigger>

                &#x3C;!-- Disabled state -->
                &#x3C;Trigger Property="IsEnabled" Value="False">
                    &#x3C;Setter Property="Opacity" Value="0.5"/>
                &#x3C;/Trigger>
            &#x3C;/ControlTemplate.Triggers>
        &#x3C;/ControlTemplate>
    &#x3C;/Setter.Value>
&#x3C;/Setter>

</Style>

  1. PART_ Naming Convention

Used when code-behind in CustomControl needs to access specific elements:

<!-- Mark required elements with PART_ prefix --> <Border x:Name="PART_Border"/> <ContentPresenter x:Name="PART_ContentHost"/> <Popup x:Name="PART_Popup"/>

// Find PART_ elements in OnApplyTemplate public override void OnApplyTemplate() { base.OnApplyTemplate();

var border = GetTemplateChild("PART_Border") as Border;
var popup = GetTemplateChild("PART_Popup") as Popup;

}

  1. Checklist
  • Specify TargetType

  • Connect properties with TemplateBinding

  • Set RecognizesAccessKey="True" on ContentPresenter

  • Handle IsMouseOver, IsPressed, IsEnabled, IsFocused triggers

  • Mark required elements with PART_ naming

  • Specify default property values with Style's Setter

  1. References
  • ControlTemplate - Microsoft Docs

  • Styling and Templating - Microsoft Docs

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

converting-html-css-to-wpf-xaml

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

managing-styles-resourcedictionary

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

using-xaml-property-element-syntax

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

designing-avalonia-customcontrol-architecture

No summary provided by upstream source.

Repository SourceNeeds Review