WPF
Dependency Property
Attached Property
.NET
Software Development

What's the difference between a dependency property and an attached property in WPF?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In WPF, an attached property is not a completely separate property system from a dependency property. It is a special kind of dependency property that is registered so one type can set a value on other objects. The practical difference is about ownership and intended usage, not about whether the value participates in the WPF property system.

Dependency Properties Are WPF-Native Properties

A dependency property is a property registered with WPF so it can participate in features such as:

  • data binding
  • styling
  • animation
  • default values
  • property value inheritance
  • change callbacks

A normal CLR property does not automatically get those WPF capabilities.

Example of a custom dependency property:

csharp
1using System.Windows;
2using System.Windows.Controls;
3
4public class HighlightBox : Control
5{
6    public static readonly DependencyProperty IsHighlightedProperty =
7        DependencyProperty.Register(
8            nameof(IsHighlighted),
9            typeof(bool),
10            typeof(HighlightBox),
11            new PropertyMetadata(false));
12
13    public bool IsHighlighted
14    {
15        get => (bool)GetValue(IsHighlightedProperty);
16        set => SetValue(IsHighlightedProperty, value);
17    }
18}

This property belongs to HighlightBox. That is the normal dependency-property pattern.

Attached Properties Are Meant to Be Set on Other Objects

An attached property is registered differently because its purpose is usually to let one class store or interpret property values on another class.

The classic example is Grid.Row:

xml
<Grid>
    <Button Content="Save" Grid.Row="1" Grid.Column="2" />
</Grid>

The Button does not own a Row CLR property. The Grid class defines an attached property that child elements can carry so the parent grid knows how to lay them out.

How Attached Properties Are Declared

Attached properties use RegisterAttached and static getter and setter methods.

csharp
1using System.Windows;
2
3public static class LayoutHints
4{
5    public static readonly DependencyProperty PriorityProperty =
6        DependencyProperty.RegisterAttached(
7            "Priority",
8            typeof(int),
9            typeof(LayoutHints),
10            new PropertyMetadata(0));
11
12    public static void SetPriority(UIElement element, int value)
13    {
14        element.SetValue(PriorityProperty, value);
15    }
16
17    public static int GetPriority(UIElement element)
18    {
19        return (int)element.GetValue(PriorityProperty);
20    }
21}

Usage in XAML:

xml
<Button Content="Save" local:LayoutHints.Priority="10" />

The button stores the value, but the defining class is LayoutHints.

Both Still Use the Same WPF Property Engine

This is the most important conceptual point: attached properties are still dependency properties under the hood. They support the same WPF property-system features, because they are registered into that same system.

So the wrong mental model is:

  • dependency property versus attached property as unrelated things

The better model is:

  • normal dependency property: owned and typically used directly by the declaring type
  • attached property: registered so other objects can carry the value too

When to Use a Normal Dependency Property

Use a normal dependency property when the property is part of the object's own API.

Examples:

  • 'Button.IsDefault'
  • 'TextBox.Text'
  • a custom control's IsHighlighted

In these cases, the object conceptually owns the property, and the property is naturally accessed as part of that object's behavior.

When to Use an Attached Property

Use an attached property when another object, often a parent, service, or behavior system, needs metadata on a target object.

Typical examples:

  • layout metadata such as Grid.Row
  • behavior flags used by helper classes
  • cross-cutting UI hints applied to arbitrary controls

The attached form is useful precisely because the target object does not need to define the property in its own class.

Many Attached Properties Are Also Service-Like

Some attached properties are not only for layout. They let helper classes attach extra behavior or state to arbitrary elements.

That means an attached property can act like a small extension point for the visual tree without subclassing every control involved.

The XAML Syntax Makes the Difference Visible

XAML shows the distinction very clearly.

Normal dependency property:

xml
<TextBox Text="Hello" />

Attached property:

xml
<TextBox Grid.Row="1" />

Text belongs to TextBox. Grid.Row belongs to Grid but is attached to the TextBox instance.

Common Pitfalls

  • Thinking attached properties are separate from dependency properties rather than a specialized form of them.
  • Using an attached property when the property really belongs to the control's own API.
  • Forgetting the required static Get and Set methods for custom attached properties.
  • Assuming attached properties are only for layout when they can also support behaviors and services.
  • Overusing attached properties for ordinary object design problems that would be clearer with normal properties.

Summary

  • A dependency property is a WPF property registered with the dependency-property system.
  • An attached property is a dependency property intended to be set on other objects.
  • Normal dependency properties belong to the owning control's API.
  • Attached properties are commonly used for layout and cross-cutting UI metadata.
  • The real distinction is ownership and usage pattern, not a separate runtime mechanism.

Course illustration
Course illustration

All Rights Reserved.