DependencyPropertyKey 类

定义

提供依赖项对象标识符,以便对只读依赖项对象进行有限的写访问。

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

示例

以下示例注册只读依赖属性,并在其他类成员中将 密钥用于两个目的:实现 get“包装器”,以及作为基于其他属性值计算设置值的受保护确定操作的标识符。

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

注解

DependencyPropertyKey 实例作为依赖属性注册调用的返回值使用 或 RegisterReadOnlyRegisterAttachedReadOnly获取。

注册依赖属性的类型可以在 DependencyPropertyKey 调用 SetValue 中使用 ,并 ClearValue 调整属性的值作为类逻辑的一部分。 如果密钥的访问级别允许,相关类也可以使用密钥和依赖属性。 例如,可以将密钥声明为 internal,同一程序集中的其他类型也可以设置该依赖属性。

DependencyPropertyKey只读依赖属性注册返回的 不应公开,因为公开密钥会使属性可设置,因此无法将其注册为只读依赖属性。 此外,公开密钥会导致可用的依赖属性行为与其公共语言运行时 (CLR) 属性包装器实现不匹配,这是错误的类设计。

不应公开键本身,而应将 的值公开DependencyPropertyDependencyPropertyKeypublic static readonlyDependencyProperty类上的 。 这样,属性就可以为某些属性系统操作(例如枚举本地设置的值)返回有效的依赖属性标识符。 但是,对于许多属性系统操作,获取的标识符并不具有 的完整功能 DependencyProperty

属性

DependencyProperty

获取与此专用只读依赖项对象标识符关联的依赖项对象标识符。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

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

重写由此依赖项对象标识符表示的只读依赖项对象的元数据。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅