Use the following code to register the dependency properties:
p>
public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata);
Logically speaking, it just combines the property name with the owner Types are associated.
So, if I have multiple instances of the owner type, and each instance sets the DP to a different value.
How to store these values?
Update 1-October 30, 2013 at 10:04 am
I read about the attachment 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< br> 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:
Obviously we cannot give all the alignment properties, such as Top, Left to Button. Therefore, Canvas defines these properties and “attaches” them to the Button control.
When Canvas.Top is designated as the “property” of the Button in XAML, it will call the SetTop() method defined in the Canvas type. Button is passed in as an element parameter.
I think this is how Canvas knows which Button to use which Top value.
public static void SetTop(UIElement element, double length);
But I don’t understand why the dependent property must be a dependent property? What is the connection between them?
Thank you!
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
"Items", typeof(ObservableCollection) , typeof(MainWindow),
new UIPropertyMetadata(new ObservableCollection()));
public ObservableCollectionItems
{
get {return ( ObservableCollection)GetValue(ItemsProperty); }
set {SetValue(ItemsProperty, value); }
}
Here you can see the GetValue discussed by @Clemens And SetValue method. We can access these methods in Window and/or UserControl because they all extend the DependencyObject class. You can also see that the Items property here is not static… It is just a static definition of DependencyProperty.
Update>>>
It doesn’t make much sense to ask why the dependent property must be DependencyProperty? Because in .NET, they are just…they are designed like this. A better question might be, what benefits do DependencyProperty get from DependencyProperty?
The answer is like being asked what benefits do properties get from DependencyProperty? The main benefit is that these properties can be used for bindings, styles, animations, resources, etc. More details can be found from two very important pages on MSDN (already linked to the comments), suitable for any WPF developer: /p>
Dependency Properties Overview
Attached Properties Overview
(I am completely new to this concept, so I may ask the very basic Question.)
Use the following code to register dependency properties:
public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata);
Logically, it just associates the attribute name with the owner type.
So if I have multiple instances of the owner type, and each The examples all set DP to different values.
How to store these values?
Update 1-October 30, 2013 at 10:04 am
I read about the attachment 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< br> 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:
Obviously we cannot give all the alignment properties, such as Top, Left to Button. Therefore, Canvas defines these properties and “attaches” them to the Button control.
When Canvas.Top is When specified as the “property” of the Button in XAML, it will call the SetTop() method defined in the Canvas type. Button is passed in as an element parameter.
I think this is how Canvas knows which Button uses which Top value. p>
public static void SetTop(UIElement element, double length);
But I don’t understand why the dependent property must be a dependent property? What is the connection between them?
Thank you!
Usually when we define DependencyProperty, we also define a CLR’wrapper’, which enables us to use DependencyProperty in our code:
< /p>
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
"Items", typeof(ObservableCollection), typeof(MainWindow),
new UIPropertyMetadata (new ObservableCollection()));
public ObservableCollectionItems
{
get {return (ObservableCollection)GetValue(ItemsProperty); }< br /> set {SetValue(ItemsProperty, value); }
}
Here, you can see the GetValue and SetValue methods discussed by @Clemens. We can set it in Window and/or UserControl Access these methods in, because they all extend the DependencyObject class. You can also see that the Items property here is not static… It is just a static definition of DependencyProperty.
Update>>>
< p>It doesn’t make much sense to ask why the dependent property must be DependencyProperty? Because in .NET, they are just…they are designed like this. A better question might be, what benefits do DependencyProperty get from DependencyProperty?
The answer is like being asked what benefits do properties get from DependencyProperty? The main benefit is that these properties can be used for bindings, styles, animations, resources, etc. More details can be found from two very important pages on MSDN (already linked to the comments), suitable for any WPF developer: /p>
Dependency Properties Overview
Attached Properties Overview