DependencyPropertyKey.DependencyProperty 属性

定义

获取与此专用只读依赖属性标识符关联的依赖属性标识符。

public:
 property System::Windows::DependencyProperty ^ DependencyProperty { System::Windows::DependencyProperty ^ get(); };
public System.Windows.DependencyProperty DependencyProperty { get; }
member this.DependencyProperty : System.Windows.DependencyProperty
Public ReadOnly Property DependencyProperty As DependencyProperty

属性值

相关的依赖属性标识符。

示例

以下示例调用DependencyProperty公开DependencyProperty类上只读依赖属性的AquariumGraphicProperty标识符 (AquariumGraphic)。 该示例还显示DependencyPropertyKey创建(作为内部成员)和 get 访问器。AquariumGraphic

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

注解

该值 DependencyProperty 使只读属性的标识符能够使用一些用于读写依赖项属性的相同接口参与通用属性系统操作。

若要实现只读依赖属性的 get 属性访问器,应在类上创建并公开 DependencyProperty 标识符。 这有两个用途:

  • 你自己的类需要 DependencyProperty 标识符才能实现属性包装器的 get 访问器。 将 DependencyProperty 用作实现 get 访问器的调用的参数 GetValue

  • DependencyProperty 标识符将依赖属性公开给属性系统,以便依赖元数据的其他方法可以采用标准形式访问它。 例如,如果对某些GetLocalValueEnumerator属性调用DependencyObject并获取了本地集属性(值和标识符)的枚举,则为只读依赖属性返回的标识符将是你的DependencyProperty值,而不是密钥。 不公开 DependencyProperty 标识符不会以任何方式增加只读依赖属性的安全性,它只会使涉及属性的操作在后续派生类和类实例中更加尴尬。

若要公开 DependencyProperty 类上的标识符,请直接调用 DependencyProperty 密钥。 使用此值在类上创建一个 public static readonlyDependencyProperty 标识符,该标识符与 DependencyPropertyKey..

适用于

另请参阅