(I am totally new to this concept so I may be asking very basic questions.)
A dependency property is registered with the code below:
public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata);
Logically, it did nothing but associate a property name with the owner type.
So if I have multiple instances of the owner type, and each instance set the DP to different values.
How could these values be stored?
I read about the Attached Property from here: http://wpftutorial.net/DependencyProperties.html
Attached Properties
Attached properties are a special kind of DependencyProperties. They allow you to attach a value to an object that does not know anything about this value.
A good example for this concept are layout panels. Each layout panel needs different data to align its child elements. The Canvas needs Top and Left, The DockPanel needs Dock, etc. Since you can write your own layout panel, the list is infinite. So you see, it's not possible to have all those properties on all WPF controls.
The solution are attached properties. They are defined by the control that needs the data from another control in a specific context. For example an element that is aligned by a parent layout panel.
So in the following code snippet:
<Canvas>
<Button Canvas.Top="20" Canvas.Left="20" Content="Click me!"/>
<Button Canvas.Top="40" Canvas.Left="20" Content="Click me!"/>
</Canvas>
Apparently we cannot give all the align properties such as Top, Left to Button. So Canvas defines such properties and they are "attached" to Button control.
When Canvas.Top is specified as an "attribute" of the Button in XAML, it will invoke the SetTop() method which is defined in the Canvas type. And the Button is passed in as the element argument. I think that's how Canvas knows which Button use which Top value.
public static void SetTop(UIElement element, double length);
But I don't see why the Attached Property has to be a Dependency Property? What's the connection between them?
Thanks!
A dependency property can reference a value through data binding. Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, determination of the final property value is deferred until run time, at which time the value is obtained from a data source.
Why We Need Dependency Properties. Basically, Dependency Properties offer a lot of functionalities that you won't get by using a CLR property. CLR properties can directly read/write from the private member of a class by using getter and setter. In contrast, dependency properties are not stored in local object.
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.
The DependencyObject class enables Windows Presentation Foundation (WPF) property system services on its many derived classes. The property system's primary function is to compute the values of properties, and to provide system notification about values that have changed.
Usually when we define a DependencyProperty
, we also define a CLR 'wrapper' that enables us to use the DependencyProperty
in code:
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
"Items", typeof(ObservableCollection<string>), typeof(MainWindow),
new UIPropertyMetadata(new ObservableCollection<string>()));
public ObservableCollection<string> Items
{
get { return (ObservableCollection<string>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
Here you can see the GetValue
and SetValue
methods that @Clemens was talking about. We get access to these methods in a Window
and/or UserControl
because they both extend the DependencyObject
class. You can also see that the Items
property here is not static
... it is just the definition of the DependencyProperty
that is static
.
UPDATE >>>
There's not really much point in asking why does an Attached Property have to be a DependencyProperty
? because in .NET, they just are... they were just designed like that. A better question might be, what benefit does an Attached Property get from being a DependencyProperty
?
The answer to that would be the same as if asked what benefit does a property get from being a DependencyProperty
? The main benefits being that these properties can be used in Binding
s, Style
s, Animation
s and Resources
among other things. More details can be found from the (already linked to in the comments) two very important pages on MSDN for any WPF developers:
Dependency Properties Overview
Attached Properties Overview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With