DependencyProperty.RegisterAttached method

Applies to Windows and Windows Phone

Registers an attached dependency property with the specified property name, property type, owner type, and property metadata for the property.

Syntax


public static DependencyProperty RegisterAttached(
  string name, 
  Type propertyType, 
  Type ownerType, 
  PropertyMetadata defaultMetadata
)

Parameters

name

Type: System.String [.NET] | Platform::String [C++]

The name of the dependency property to register.

propertyType

Type: System.Type [.NET] | TypeName [C++]

The type of the property, as a type reference (System.Type for .NET, a TypeName helper struct for C++/CX).

ownerType

Type: System.Type [.NET] | TypeName [C++]

The owner type that is registering the dependency property, as a type reference (System.Type for .NET, a TypeName helper struct for C++/CX).

defaultMetadata

Type: PropertyMetadata

A property metadata instance. This can contain a PropertyChangedCallback implementation reference.

Return value

Type: DependencyProperty

A dependency property identifier that should be used to set the value of a public static read-only 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

Tip  If you are programming using a .NET language (C# or Microsoft Visual Basic), the TypeName type projects as System.Type. When programming using C#, it is common to use the typeof operator to get references to the System.Type of a type. In Visual Basic, use GetType. If you're using C++/CX), where you'll need to create a TypeName helper struct, you can use the typeid component extension.

Examples

This 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. For more examples, see Custom attached properties.


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


Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml
Windows::UI::Xaml [C++]

Metadata

Windows.winmd

See also

DependencyProperty
Custom attached properties
Dependency properties overview
Attached properties overview

 

 

Show:
© 2014 Microsoft