#574 – Complete Example of Implementing a Dependency Property

Recall that you can implement a dependency property if your class inherits from DependencyObject.  Here is a complete example, showing how to implement a dependency property.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;   // Remember to reference WindowsBase

namespace PersonDLL
    public class Person : DependencyObject
        // Classic CLR properties
        public string Name { get; set; }
        public bool AARPCandidate { get; set; }

        // Age is a dependency property
        public static readonly DependencyProperty AgeProperty;

        // Call methods in DependencyObject to read/write property values
        public int Age
            get { return (int)GetValue(AgeProperty); }
            set { SetValue(AgeProperty, value); }

        // Static constructor sets everything up
        static Person()
            PropertyMetadata ageMetadata =
                new PropertyMetadata(
                    18,     // Default value
                    new PropertyChangedCallback(OnAgeChanged),
                    new CoerceValueCallback(OnAgeCoerceValue));

            // Register the property
            AgeProperty =
                    "Age",                 // Property's name
                    typeof(int),           // Property's type
                    typeof(Person),        // Defining class' type
                    ageMetadata,           // Defines default value & callbacks  (optional)
                    new ValidateValueCallback(OnAgeValidateValue));   // validation (optional)

        // Value has changed
        private static void OnAgeChanged
            (DependencyObject depObj, DependencyPropertyChangedEventArgs e)
            Person p = (Person)depObj;
            p.AARPCandidate = (int)e.NewValue > 60 ? true : false;

        // Allow coercing value being set
        private static object OnAgeCoerceValue
            (DependencyObject depObj, object baseValue)
            int coercedValue = (int)baseValue;

            if ((int)baseValue > 120)
                coercedValue = 120;

            if ((int)baseValue < 1)
                coercedValue = 1;

            return coercedValue;

        // Validate a value beint set
        private static bool OnAgeValidateValue(object value)
            int age = (int)value;

            // Only allow reasonable ages
            return (age > 0) && (age < 120);

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

4 Responses to #574 – Complete Example of Implementing a Dependency Property

  1. Jasper says:

    Hey, I like that you always come up with original examples, rather than just copying from MSDN like some sites (e.g. http://jasper-net.blogspot.com).. Nice job!

  2. Pingback: #575 – PropertyMetadata vs. FrameworkPropertyMetadata « 2,000 Things You Should Know About WPF

  3. Dan Maroff says:

    Excellent example. Very clean code; a pleasure to read.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: