What is a dependency property in .Net (especially in WPF context). What is the difference from the regular property?
.net – a dependency property
dependency-propertiesnetworkflow-foundation
Related Topic
- C# – the correct way to create a single-instance WPF application
- C# – the difference between const and readonly in C#
- C# – What are the correct version numbers for C#
- .net – the best algorithm for overriding GetHashCode
- C# – How to get a consistent byte representation of strings in C# without manually specifying an encoding
- .net – Difference between decimal, float and double in .NET
- .net – Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
- .net – the difference between .NET Core and .NET Standard Class Library project types
Best Answer
The only explanation I found helpful and well written is this one: http://www.wpftutorial.net/dependencyproperties.html
Basically, DependencyProperties differ from regular properties in that they're not just setters / getters for fields in the class, but they retrieve their actual values dynamically during runtime. The
SetValue()
method of DPs is pretty straightforward and sets the local value of the property to the value you gave it. However, when you try toGetValue()
from a DependencyProperty, it will first look for a local value, if none is present (which is viable in DependencyProperties unlike regular properties) it will continue up the logical UI tree until it will find such value. If the framework has reached the top of the tree without finding any local values, it will then use a predefined default value as the property's value.This method allows DependencyProperties to consume less memory than regular properties since only values that were explicitly set by the user will be stored locally.
And, as mentioned above, DependencyProperties also allow us to bind to them in the XAML code and set triggers on them, which on regular properties is not allowed.
I hope I've managed to clear some of the vagueness :)