DependencyProperty.RegisterAttached Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Registers an attached dependency property with the specified property name, property type, owner type, and property metadata for the property.
Namespace: System.Windows
Assembly: System.Windows (in System.Windows.dll)
Syntax
'Declaration
Public Shared Function RegisterAttached ( _
name As String, _
propertyType As Type, _
ownerType As Type, _
defaultMetadata As PropertyMetadata _
) As DependencyProperty
public static DependencyProperty RegisterAttached(
string name,
Type propertyType,
Type ownerType,
PropertyMetadata defaultMetadata
)
Parameters
- name
Type: System.String
The name of the dependency property to register.
- propertyType
Type: System.Type
The type of the property.
- ownerType
Type: System.Type
The owner type that is registering the dependency property.
- defaultMetadata
Type: System.Windows.PropertyMetadata
A property metadata instance. This can contain a PropertyChangedCallback implementation reference.
Return Value
Type: System.Windows.DependencyProperty
A dependency property identifier that should be used to set the value of a public static readonly field in your class. That identifier is then used to reference the attached property later, for operations such as setting its value programmatically, or attaching a Binding.
Remarks
Silverlight follows the WPF model of declaring all its attached properties using the dependency property system, such that each has a DependencyProperty identifier.
In Silverlight, a value for an attached property can only be set on objects of type DependencyObject, because of the DependencyProperty property store that DependencyObject maintains. Further restrictions might be imposed by specific attached properties, reflecting their usage scenarios.
Registering the property and exposing the dependency property identifier is not all that is needed in order to define a XAML-settable custom attached property, which is the most typical scenario for defining an attached property. You also must define a Set method accessor (and typically you define a Get method accessor, so that the owning type can use the values as set on other objects). This is required so that a XAML processor can set values from attached property usages found in the XAML. For setting or getting values in code, you generally could use the utility methods GetValue or SetValue instead, but the dedicated Get and Set methods for attached property support are typically more robust, because they can specify more exact type constraints on the values than the open-typed GetValue / SetValue. For details on how to write Get and Set methods for attached property support, and how to call existing implementations, see Custom Attached Properties or Attached Properties Overview.
Examples
The following example defines a class that derives from DependencyObject, and defines an attached property along with the identifier field. The scenario for this class is that it is a service class that declares an attached property that other UI elements can set in XAML, and the service potentially acts on the attached property values on those UI elements at run time.
public abstract class AquariumServices : DependencyObject
{
public enum Bouyancy {Floats,Sinks,Drifts}
public static readonly DependencyProperty BouyancyProperty = DependencyProperty.RegisterAttached(
"Bouyancy",
typeof(Bouyancy),
typeof(AquariumServices),
new PropertyMetadata(Bouyancy.Floats)
);
public static void SetBouyancy(DependencyObject element, Bouyancy value)
{
element.SetValue(BouyancyProperty, value);
}
public static Bouyancy GetBouyancy(DependencyObject element)
{
return (Bouyancy)element.GetValue(BouyancyProperty);
}
}
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
See Also