Skip to main content
.NET Framework Class Library
DependencyPropertyKey Class

Provides a dependency property identifier for limited write access to a read-only dependency property.

Inheritance Hierarchy
SystemObject
  System.WindowsDependencyPropertyKey

Namespace:   System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)
Syntax
Public NotInheritable Class DependencyPropertyKey
public sealed class DependencyPropertyKey
public ref class DependencyPropertyKey sealed
[<[%$TOPIC/ms602348_en-us_VS_110_2_0_3_0_0%]>]
type DependencyPropertyKey =  class end

The DependencyPropertyKey type exposes the following members.

Properties
  NameDescription
Public property DependencyPropertyGets the dependency property identifier associated with this specialized read-only dependency property identifier.
Top
Methods
  NameDescription
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method OverrideMetadataOverrides the metadata of a read-only dependency property that is represented by this dependency property identifier.
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Remarks

DependencyPropertyKey instances are obtained as the return value of a dependency property registration call using the methods RegisterReadOnly or RegisterAttachedReadOnly.

The types that register a dependency property can use the DependencyPropertyKey in calls to SetValue and ClearValue that adjust the property's value as part of class logic. If permitted by the access level of the key, related classes can use the key and the dependency property also. For instance, you can declare the key as internal, and other types within the same assembly can also set that dependency property.

The DependencyPropertyKey returned by read-only dependency property registration should not be made public, because exposing the key makes the property settable, thus defeating the point of registering it as a read-only dependency property. Also, exposing the key causes a mismatch between the available dependency property behaviors and its common language runtime (CLR) property wrapper implementations, which is bad class design.

Instead of exposing the key itself, you should instead expose the DependencyProperty value of the DependencyPropertyKey as a public static readonly DependencyProperty on your class. This enables the property to return a valid dependency property identifier for certain property system operations such as enumerating locally set values. However, the identifier thus obtained does not have the full capabilities of a DependencyProperty for many property system operations.

Examples

The following example registers a read-only dependency property, and also uses the key for two purposes in other class members: implementing the get "wrapper", and as the identifier for a protected determination operation that sets the value based on calculations of other property values.

Friend Shared ReadOnly AquariumSizeKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("AquariumSize", GetType(Double), GetType(Aquarium), New PropertyMetadata(Double.NaN))
Public Shared ReadOnly AquariumSizeProperty As DependencyProperty = AquariumSizeKey.DependencyProperty
Public ReadOnly Property AquariumSize() As Double
    Get
        Return CDbl(GetValue(AquariumSizeProperty))
    End Get
End Property
internal static readonly DependencyPropertyKey AquariumSizeKey = DependencyProperty.RegisterReadOnly(
  "AquariumSize",
  typeof(double),
  typeof(Aquarium),
  new PropertyMetadata(double.NaN)
);
public static readonly DependencyProperty AquariumSizeProperty =
  AquariumSizeKey.DependencyProperty;
public double AquariumSize
{
  get { return (double)GetValue(AquariumSizeProperty); }
}
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.