#118 – Don’t Add Code to Dependency Property Getter/Setter

When you implement your own dependency property, you typically include getter/setter methods that just wrap called to DependencyObject.GetValue and DependencyObject.SetValue.  This allows the dependency property to be used like a regular CLR property.  Here’s an example:

        public int Age
        {
            get { return (int)GetValue(AgeProperty); }
            set { SetValue(AgeProperty, value); }
        }

You should never include any code in these methods other than the calls to GetValue and SetValue.  When you get/set property values from XAML, WPF will end up calling the GetValue and SetValue methods directly, bypassing your getter and setter.  You should include all behavior related to getting/setting the property value in the appropriate callbacks that you specify when you register the property.

Advertisements