Editar

Compartir vía


DependencyProperty.RegisterAttached Method

Definition

Registers an attached property with the property system.

Overloads

RegisterAttached(String, Type, Type)

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

RegisterAttached(String, Type, Type, PropertyMetadata)

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

RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback)

Registers an attached property with the specified property type, owner type, property metadata, and value validation callback for the property.

RegisterAttached(String, Type, Type)

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

public:
 static System::Windows::DependencyProperty ^ RegisterAttached(System::String ^ name, Type ^ propertyType, Type ^ ownerType);
public static System.Windows.DependencyProperty RegisterAttached (string name, Type propertyType, Type ownerType);
static member RegisterAttached : string * Type * Type -> System.Windows.DependencyProperty
Public Shared Function RegisterAttached (name As String, propertyType As Type, ownerType As Type) As DependencyProperty

Parameters

name
String

The name of the dependency property to register.

propertyType
Type

The type of the property.

ownerType
Type

The owner type that is registering the dependency property.

Returns

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 dependency property later, for operations such as setting its value programmatically or obtaining metadata.

Examples

The following example registers an attached property on an abstract class using this RegisterAttached signature.

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
  "IsBubbleSource",
  typeof(Boolean),
  typeof(AquariumObject2)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
    element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
    return (Boolean)element.GetValue(IsBubbleSourceProperty);
}
Public Shared ReadOnly IsBubbleSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsBubbleSource", GetType(Boolean), GetType(AquariumObject2))
Public Shared Sub SetIsBubbleSource(ByVal element As UIElement, ByVal value As Boolean)
    element.SetValue(IsBubbleSourceProperty, value)
End Sub
Public Shared Function GetIsBubbleSource(ByVal element As UIElement) As Boolean
    Return CType(element.GetValue(IsBubbleSourceProperty), Boolean)
End Function

Remarks

An attached property is a property concept defined by Extensible Application Markup Language (XAML). WPF implements attached properties as dependency properties. Because the WPF attached properties are dependency properties, they can have metadata applied that can be used by the general property system for operations such as reporting layout characteristics. For more information, see Attached Properties Overview.

For more information on dependency property registration, see DependencyProperty.

See also

Applies to

RegisterAttached(String, Type, Type, PropertyMetadata)

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

public:
 static System::Windows::DependencyProperty ^ RegisterAttached(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ defaultMetadata);
public static System.Windows.DependencyProperty RegisterAttached (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata defaultMetadata);
static member RegisterAttached : string * Type * Type * System.Windows.PropertyMetadata -> System.Windows.DependencyProperty
Public Shared Function RegisterAttached (name As String, propertyType As Type, ownerType As Type, defaultMetadata As PropertyMetadata) As DependencyProperty

Parameters

name
String

The name of the dependency property to register.

propertyType
Type

The type of the property.

ownerType
Type

The owner type that is registering the dependency property.

defaultMetadata
PropertyMetadata

Property metadata for the dependency property. This can include the default value as well as other characteristics.

Returns

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 dependency property later, for operations such as setting its value programmatically or obtaining metadata.

Remarks

An attached property is a property concept defined by Extensible Application Markup Language (XAML). WPF implements attached properties as dependency properties. Because the WPF attached properties are dependency properties, they can have metadata applied that can be used by the general property system for operations such as reporting layout characteristics. For more information, see Attached Properties Overview.

For more information on dependency property registration, see DependencyProperty.

Use RegisterAttached for Value-inheriting Dependency Properties

One particular scenario for registering a dependency property with RegisterAttached instead of Register is to support property value inheritance. You should register value-inheriting dependency properties with RegisterAttached even if the class defines property wrapper accessors that expose the dependency property, and even if you do not intend to expose Get* and Set* static methods to provide true attached property support accessors. Although property value inheritance might appear to work for nonattached dependency properties, the inheritance behavior for a nonattached property through certain element boundaries in the runtime tree is undefined. Registering the property as attached effectively makes the attached property a global property to the property system, and assures that property value inheritance works across all boundaries in an element tree. Always use RegisterAttached to register properties where you specify Inherits in the metadata. For more information, see Property Value Inheritance.

See also

Applies to

RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback)

Registers an attached property with the specified property type, owner type, property metadata, and value validation callback for the property.

public:
 static System::Windows::DependencyProperty ^ RegisterAttached(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ defaultMetadata, System::Windows::ValidateValueCallback ^ validateValueCallback);
public static System.Windows.DependencyProperty RegisterAttached (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata defaultMetadata, System.Windows.ValidateValueCallback validateValueCallback);
static member RegisterAttached : string * Type * Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyProperty
Public Shared Function RegisterAttached (name As String, propertyType As Type, ownerType As Type, defaultMetadata As PropertyMetadata, validateValueCallback As ValidateValueCallback) As DependencyProperty

Parameters

name
String

The name of the dependency property to register.

propertyType
Type

The type of the property.

ownerType
Type

The owner type that is registering the dependency property.

defaultMetadata
PropertyMetadata

Property metadata for the dependency property. This can include the default value as well as other characteristics.

validateValueCallback
ValidateValueCallback

A reference to a callback that should perform any custom validation of the dependency property value beyond typical type validation.

Returns

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 dependency property later, for operations such as setting its value programmatically or obtaining metadata.

Examples

The following example registers an attached property on an abstract class using this RegisterAttached signature. This attached property is an enumeration type property, and the registration adds a validation callback to verify that the provided value is a value of the enumeration.

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
  "IsBubbleSource",
  typeof(Boolean),
  typeof(AquariumObject2)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
    element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
    return (Boolean)element.GetValue(IsBubbleSourceProperty);
}
Public Shared ReadOnly IsBubbleSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsBubbleSource", GetType(Boolean), GetType(AquariumObject2))
Public Shared Sub SetIsBubbleSource(ByVal element As UIElement, ByVal value As Boolean)
    element.SetValue(IsBubbleSourceProperty, value)
End Sub
Public Shared Function GetIsBubbleSource(ByVal element As UIElement) As Boolean
    Return CType(element.GetValue(IsBubbleSourceProperty), Boolean)
End Function

Remarks

An attached property is a property concept defined by Extensible Application Markup Language (XAML). WPF implements attached properties as dependency properties. Because the WPF attached properties are dependency properties, they can have metadata applied that can be used by the general property system for operations such as reporting layout characteristics. For more information, see Attached Properties Overview.

For more information on dependency property registration, see DependencyProperty.

Use RegisterAttached for Value-inheriting Dependency Properties

One particular scenario for registering a dependency property with RegisterAttached instead of Register is to support property value inheritance. You should register value-inheriting dependency properties with RegisterAttached even if the class defines property wrapper accessors that expose the dependency property, and even if you do not intend to expose Get* and Set* static methods to provide true attached property support accessors. Although property value inheritance might appear to work for nonattached dependency properties, the inheritance behavior for a nonattached property through certain element boundaries in the runtime tree is undefined. Registering the property as attached effectively makes the attached property a global property to the property system, and assures that property value inheritance works across all boundaries in an element tree. Always use RegisterAttached to register properties where you specify Inherits in the metadata. For more information, see Property Value Inheritance.

See also

Applies to