This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge
public:
 static System::Windows::DependencyPropertyKey ^ RegisterAttachedReadOnly(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ defaultMetadata);
public static System.Windows.DependencyPropertyKey RegisterAttachedReadOnly (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata defaultMetadata);
static member RegisterAttachedReadOnly : string * Type * Type * System.Windows.PropertyMetadata -> System.Windows.DependencyPropertyKey
Public Shared Function RegisterAttachedReadOnly (name As String, propertyType As Type, ownerType As Type, defaultMetadata As PropertyMetadata) As DependencyPropertyKey

Parameters

Remarks

This method returns the type DependencyPropertyKey , whereas RegisterAttached returns the type DependencyProperty . Typically, the keys that represent read-only properties are not made public, because the keys can be used to set the dependency property value by calling SetValue(DependencyPropertyKey, Object) . Your class design will affect your requirements, but it is generally recommended to limit the access and visibility of any DependencyPropertyKey to only those parts of your code that are necessary to set that dependency property as part of class or application logic. It is also recommended that you expose a dependency property identifier for the read-only dependency property, by exposing the value of DependencyPropertyKey.DependencyProperty as a public static readonly field on your class.

Read-only attached properties are a rare scenario, because the primary scenario for an attached property is its use in XAML. Without a public setter, an attached property cannot be set in XAML syntax.

For more information on dependency property registration, see DependencyProperty .

Use RegisterAttached for Value-inheriting Dependency Properties

One particular scenario for registering a dependency property as attached 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 .

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

Parameters

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

Returns

Remarks

This method returns the type DependencyPropertyKey , whereas RegisterAttached returns the type DependencyProperty . Typically, the keys that represent the type DependencyProperty . Typically, the keys that represent read-only properties are not made public, because the keys can be used to set the dependency property value by calling SetValue(DependencyPropertyKey, Object) . Your class design will affect your requirements, but it is generally recommended to limit the access and visibility of any DependencyPropertyKey to only those parts of your code that are necessary to set that dependency property as part of class or application logic. It is also recommended that you expose a dependency property identifier for the read-only dependency property, by exposing the value of DependencyPropertyKey.DependencyProperty as a public static readonly field on your class.

Read-only attached properties are a rare scenario, because the primary scenario for an attached property is its use in XAML. Without a public setter, an attached property cannot be set in XAML syntax.

For more information on dependency property registration, see DependencyProperty .

Use RegisterAttached for Value-inheriting Dependency Properties

One particular scenario for registering a dependency property as attached 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 .