DependencyProperty.RegisterAttached Method

July 28, 2014

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)

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.

Windows Phone follows the WPF model of declaring all its attached properties using the dependency property system, such that each has a DependencyProperty identifier.

In Windows Phone, 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.

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);
    }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft