DependencyPropertyKey 类


提供依赖项对象标识符,以便对只读依赖项对象进行有限的写访问。Provides a dependency property identifier for limited write access to a read-only dependency property.

public ref class DependencyPropertyKey sealed
public sealed class DependencyPropertyKey
type DependencyPropertyKey = class
Public NotInheritable Class DependencyPropertyKey


下面的示例将注册一个只读依赖属性, 并将该密钥用于其他类成员中的两个目的: 实现 get "包装器", 并将其用作基于计算设置值的受保护确定操作的标识符其他属性值。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.

internal static readonly DependencyPropertyKey AquariumSizeKey = DependencyProperty.RegisterReadOnly(
  new PropertyMetadata(double.NaN)
public static readonly DependencyProperty AquariumSizeProperty =
public double AquariumSize
  get { return (double)GetValue(AquariumSizeProperty); }
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
        Return CDbl(GetValue(AquariumSizeProperty))
    End Get
End Property


DependencyPropertyKey使用方法RegisterReadOnlyRegisterAttachedReadOnly获取实例作为依赖属性注册调用的返回值。DependencyPropertyKey instances are obtained as the return value of a dependency property registration call using the methods RegisterReadOnly or RegisterAttachedReadOnly.

注册依赖项属性的类型可以DependencyPropertyKey在对SetValue的调用中使用, ClearValue并且可以将属性的值作为类逻辑的一部分进行调整。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.

DependencyPropertyKey应将由只读依赖项属性注册返回的返回为公共的, 因为公开密钥会使属性可设置, 因此, 摆将其注册为只读依赖属性的点。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. 此外, 公开密钥会导致可用的依赖项属性行为与公共语言运行时 (CLR)common language runtime (CLR)类设计错误的属性包装实现之间不匹配。Also, exposing the key causes a mismatch between the available dependency property behaviors and its 公共语言运行时 (CLR)common language runtime (CLR) property wrapper implementations, which is bad class design.

DependencyProperty将的DependencyPropertyKey值作为public static readonly 类的上的公开,而不是公开键本身。DependencyPropertyInstead of exposing the key itself, you should instead expose the DependencyProperty value of the DependencyPropertyKey as a public static readonlyDependencyProperty 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. 但是, 获取的标识符DependencyProperty对于许多属性系统操作不具有的全部功能。However, the identifier thus obtained does not have the full capabilities of a DependencyProperty for many property system operations.



获取与此专用只读依赖项对象标识符关联的依赖项对象标识符。Gets the dependency property identifier associated with this specialized read-only dependency property identifier.



确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OverrideMetadata(Type, PropertyMetadata)

重写由此依赖项对象标识符表示的只读依赖项对象的元数据。Overrides the metadata of a read-only dependency property that is represented by this dependency property identifier.


返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)