Dependency Property
WPF
.NET
Software Development
Programming Concepts

What is a dependency property?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In WPF, a dependency property is a property that participates in the WPF property system rather than existing only as a normal CLR field-backed property. That gives it abilities that ordinary properties do not have by default, such as styling, animation, data binding, value inheritance, and change notification hooks. If you write custom WPF controls, understanding dependency properties is essential because so much of the framework is built around them.

Why WPF Uses a Property System

A normal .NET property is usually just syntax over a field.

csharp
public string Title { get; set; }

That is fine for many classes, but it does not automatically integrate with WPF features such as styles, template bindings, animations, or inherited values. WPF solves that by routing certain properties through a property system that can resolve values from multiple sources.

Those sources can include:

  • local values
  • styles and templates
  • data bindings
  • animations
  • inherited values from parent elements
  • default metadata values

That layered behavior is the main reason dependency properties exist.

Registering a Dependency Property

A dependency property is declared as a static field and registered with the property system. A normal CLR wrapper is then added for convenient access.

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

The static field is what WPF uses internally. The CLR wrapper is what your code and XAML-friendly API surface use.

What You Gain From a Dependency Property

The main value is not the registration ceremony itself. It is the framework features that become available once the property participates in the system.

For example, a dependency property can be bound in XAML:

xml
<local:Spinner IsSpinning="{Binding IsBusy}" />

It can also be styled:

xml
<Style TargetType="{x:Type local:Spinner}">
    <Setter Property="IsSpinning" Value="True" />
</Style>

A normal CLR property would not support those interactions in the same WPF-native way.

Metadata and Change Callbacks

Property metadata can define more than just a default value. It can also attach callbacks that run when the property changes.

csharp
1public static readonly DependencyProperty ProgressProperty =
2    DependencyProperty.Register(
3        nameof(Progress),
4        typeof(double),
5        typeof(Spinner),
6        new PropertyMetadata(0.0, OnProgressChanged));
7
8private static void OnProgressChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
9{
10    var control = (Spinner)d;
11    // React to the new value here.
12}

This is useful when a control needs to update visuals, invalidate layout, or trigger side effects when a property changes.

Use Dependency Properties for Control Surface, Not for Everything

Not every property in a WPF class should become a dependency property. They make sense for values that need to participate in the WPF property system, especially values that are meant to be set from XAML, styled, animated, or bound.

If a value is just an internal implementation detail, a regular CLR property or field is often better. Overusing dependency properties adds complexity without gaining anything useful.

Value Precedence Matters

One subtle but important part of dependency properties is that several sources may try to provide a value at once. WPF resolves that through a precedence system.

For example, a local value on a control may override a style setter, while an animation may temporarily override both.

That is powerful, but it also means debugging a “wrong” property value is sometimes really a precedence issue rather than a registration issue.

Common Pitfalls

  • Creating dependency properties for internal state that does not need binding, styling, animation, or other WPF property-system features.
  • Forgetting the CLR wrapper and forcing callers to interact awkwardly with GetValue and SetValue directly.
  • Misunderstanding value precedence and blaming registration when a style, template, or local value is actually winning.
  • Putting instance-specific logic into the static registration field instead of using metadata callbacks correctly.
  • Treating a dependency property as if it were only a fancy field instead of a participant in a larger WPF property-resolution system.

Summary

  • A dependency property is a WPF property-system participant, not just a normal CLR property.
  • It enables binding, styling, animation, inheritance, and metadata-driven behavior.
  • You register it with DependencyProperty.Register and usually expose a CLR wrapper.
  • Dependency properties are ideal for control-facing values that must work naturally in XAML and the WPF framework.
  • They are powerful, but they should be used where the property-system integration actually matters.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.