Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why dependency properties?

Why did Microsoft go the route of making dependency properties and dependency objects instead of using reflection and maybe attributes?

like image 387
Mr Bell Avatar asked Nov 12 '09 17:11

Mr Bell


People also ask

Why dependency property is better than attached property?

Attached properties allows container to create a property which can be used by any child UI elements whereas dependency property is associated with that particular elements and can help in notification of changes and reacting to that changes.

What is the biggest feature of dependency property?

Arguably the biggest feature of a dependency property is its built-in ability to provide change notification. The motivation for adding such intelligence to properties is to enable rich functionality directly from declarative markup.

What is the difference between property and dependency property?

The primary difference between a dependency droperty and a standard clr property is that a dependency property can be the target of a binding. This allows you to tie the value of the property to a value provided by some other object.

Why dependency property is static readonly in WPF?

Using the readonly keyword has at least three effects: it informs readers of the code that the value will not change. it prevents the author from accidentally changing the value. it assists the compiler, which benefits from knowing when things will not change.


2 Answers

This helped me understand the reasoning:

The main difference is, that the value of a normal .NET property is read directly from a private member in your class, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject.

When you set a value of a dependency property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set.

The advantages of dependency properties are as follows:

Reduced memory footprint

It's a huge dissipation to store a field for each property when you think that over 90% of the properties of a UI control typically stay at its initial values. Dependency properties solve these problems by only store modified properties in the instance. The default values are stored once within the dependency property.

Value inheritance

When you access a dependency property the value is resolved by using a value resolution strategy. If no local value is set, the dependency property navigates up the logical tree until it finds a value. When you set the FontSize on the root element it applies to all textblocks below except you override the value.

Change notification

Dependency properties have a built-in change notification mechanism. By registering a callback in the property metadata you get notified, when the value of the property has been changed. This is also used by the databinding.

From: WPF Tutorials.

like image 71
Kyle Rosendo Avatar answered Sep 28 '22 07:09

Kyle Rosendo


Dependency properties solve a different usage scenario than reflection and attributes would solve.

Dependency properties provide a single, consistent API for doing things that standard properties cannot handle.

  • The allow classes to provide a large number of properties without overhead (since they're only created, statically, when used, not once per class). For
  • The dependency property system handles things like Attached Properties in a consistent manner as standard dependency properties.
  • Dependency properties provide a clean API for change tracking as well as validation.
  • Dependency properties provide property value inheritance.
  • Dependency properties provide great performance benefits when data bound.
like image 21
Reed Copsey Avatar answered Sep 28 '22 08:09

Reed Copsey