Defines WPF DependencyProperty with Register, PropertyMetadata, callbacks, and validation. Use when creating custom controls, attached properties, or properties that support data binding and styling.
View on GitHubchristian289/dotnet-with-claudecode
wpf-dev-pack
wpf-dev-pack/skills/defining-wpf-dependencyproperty/SKILL.md
January 23, 2026
Select agents to install to:
npx add-skill https://github.com/christian289/dotnet-with-claudecode/blob/main/wpf-dev-pack/skills/defining-wpf-dependencyproperty/SKILL.md -a claude-code --skill defining-wpf-dependencypropertyInstallation paths:
.claude/skills/defining-wpf-dependencyproperty/# WPF DependencyProperty Patterns
Defining dependency properties for data binding, styling, animation, and property value inheritance.
**Advanced Patterns:** See [ADVANCED.md](ADVANCED.md) for value inheritance, metadata override, and event integration.
## 1. DependencyProperty Overview
```
Standard CLR Property:
private string _name;
public string Name { get => _name; set => _name = value; }
DependencyProperty:
public static readonly DependencyProperty NameProperty = ...
public string Name { get => (string)GetValue(NameProperty); set => SetValue(NameProperty, value); }
Benefits:
✅ Data Binding
✅ Styling & Templating
✅ Animation
✅ Property Value Inheritance
✅ Default Values
✅ Change Notifications
✅ Value Coercion
✅ Validation
```
---
## 2. Basic DependencyProperty
### 2.1 Standard Registration
```csharp
namespace MyApp.Controls;
using System.Windows;
using System.Windows.Controls;
public class MyControl : Control
{
// 1. Register DependencyProperty
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
name: nameof(Title),
propertyType: typeof(string),
ownerType: typeof(MyControl),
typeMetadata: new PropertyMetadata(defaultValue: string.Empty));
// 2. CLR property wrapper
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
}
```
### 2.2 With FrameworkPropertyMetadata
```csharp
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
name: nameof(Value),
propertyType: typeof(double),
ownerType: typeof(MyControl),
typeMetadata: new FrameworkPropertyMetadata(
defaultValue: 0.0,
flags: FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
propertyChangedCallback: OnValueChanged,
coerceValueCallback: CoerceValue),