---
name: customizing-controltemplate
description: "Customizes WPF control appearance using ControlTemplate with TemplateBinding and ContentPresenter. Use when completely changing control visuals, implementing state-based feedback, or TemplatedParent binding."
---
# 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
---
## 2. Basic Implementation Patterns
### 2.1 Button ControlTemplate (XAML)
```xml
```
### 2.2 Applying ControlTemplate in Code
```csharp
namespace MyApp.Helpers;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
public static class TemplateHelper
{
///
/// Create ControlTemplate from XAML string
///
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);
}
}
```
---
## 3. TemplateBinding vs Binding
### 3.1 TemplateBinding (Recommended)
```xml
```
### 3.2 RelativeSource TemplatedParent (When Two-Way Needed)
```xml
```
### 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 |
---
## 4. ContentPresenter Details
### 4.1 Key Properties
```xml
```
### 4.2 RecognizesAccessKey
```xml
```
---
## 5. Trigger Patterns
### 5.1 Property Trigger
```xml
```
### 5.2 MultiTrigger
```xml
```
### 5.3 EventTrigger (Animation)
```xml
```
---
## 6. Practical Example: Toggle Button
```xml
```
---
## 7. PART_ Naming Convention
Used when code-behind in CustomControl needs to access specific elements:
```xml
```
```csharp
// 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;
}
```
---
## 8. 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
---
## 9. References
- [ControlTemplate - Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/controltemplate)
- [Styling and Templating - Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/styles-templates-overview)