What is the difference between a dependency property and an attached property in WPF?

What is the difference between a (dependent) dependency property and an attached property in WPF? What is the use for everyone? How are implementations usually different?

+78
wpf dependency-properties attached-properties
Aug 6 '09 at 18:52
source share
4 answers

Attached properties are a dependency property. The difference is how they are used.

With an attached property, a property is defined in a class that is not the same class for which it is used. This is usually used for layout. Good examples are Panel.ZIndex or Grid.Row — you apply this to a control (that is, a button), but it is actually defined in Panel or Grid. The property is "attached" to the button instance.

This allows the container, for example, to create properties that can be used on any UIelement.

As for the differences in implementation, it is mainly a matter of using Register vs. RegisterAttached when defining a property.

+65
Aug 06 '09 at 18:56
source share

Attached properties are mainly intended for container elements. For example, if you have a grid and you have grid.row, now this is considered an attached property of the grid element. You can also use this property in texbox, button, etc. set its place in the grid.

The Dependency property is similar to a property that basically belongs to another class and is used in another class. for example: for example, you have a rectangle here height and width are regular properties of the rectangle, but left and top are a dependency property, since it belongs to the Canvass class.

+5
Jan 10 '12 at 11:33
source share

I think you can define an attached property in the class itself or define it in another class. We could always use the attached property to extend the standard Microsoft controls. But the dependency property you define it in your own control. for example, you can inherit your control from a standard control and define a dependency property in your own control and use it. This is equivalent to defining an attached property and uses this attached property in a standard control.

0
Jun 12 '17 at 20:24
source share

Attached properties are a special kind of DependencyProperties. They allow you to bind a value to an object that does not know anything about this value. A good example for this concept is layout panels. Each layout pane needs different data to align its children. Canvas needs Top and Left, a DockPanel needs a Dock, etc. Since you can write your own layout panel, the list is endless. Therefore, you cannot have all of these properties in all WPF controls. The solution is attached properties. They are defined by a control that needs data from another control in a specific context. For example, an element that is aligned with the parent layout panel.

-2
Jul 15 '16 at 6:03
source share



All Articles