DependencyPropertyKey.DependencyProperty 属性

定义

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

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

属性值

相关的依赖项对象标识符。The relevant dependency property identifier.

示例

下面的示例调用DependencyPropertyDependencyProperty公开类上AquariumGraphic只读AquariumGraphicProperty依赖项属性的标识符 ()。The following example calls DependencyProperty to expose the DependencyProperty identifier (AquariumGraphicProperty) for the AquariumGraphic read-only dependency property on a class. 该示例还显示DependencyPropertyKey了创建 (作为内部成员) 和的 get AquariumGraphic访问器。The example also shows the DependencyPropertyKey creation (as an internal member) and the get accessor for 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值, 只读属性的标识符可以使用一些与读写依赖属性相同的接口参与到常见的属性系统操作中。The DependencyProperty value enables a read-only property's identifier to participate in common property system operations using some of the same interfaces as used for read-write dependency properties.

为了实现只读依赖项属性的 get 属性访问器, 你应在类上创建并公开DependencyProperty标识符。In order to implement the get property accessor for a read-only dependency property, you should create and expose a DependencyProperty identifier on your class. 这有两个用途:This serves two purposes:

  • 您自己的类需要DependencyProperty标识符才能实现属性包装的 get 访问器。Your own class needs the DependencyProperty identifier in order to implement the get accessor for the property wrapper. 使用DependencyProperty作为实现 get 访问器的GetValue调用的参数。You use the DependencyProperty as a parameter for the GetValue call that implements the get accessor.

  • DependencyProperty标识符向属性系统公开依赖属性, 以便其他依赖元数据的方法可以在标准窗体中访问它。DependencyProperty identifiers expose your dependency property to the property system such that other methods that rely on metadata can access it in a standard form. 例如, 如果你在某些GetLocalValueEnumerator DependencyObject上调用并获得了本地集属性 (值和标识符) 的枚举, 则为只读依赖项属性返回的标识符将是你DependencyProperty的值, 而不是密钥。For instance, if you called GetLocalValueEnumerator on some DependencyObject and obtained an enumeration of locally set properties (values and identifiers) the identifier returned for a read-only dependency property would be your DependencyProperty value rather than the key. 如果未公开DependencyProperty标识符, 则不会以任何方式增加只读依赖项属性的安全性, 它只会使涉及属性的操作对后续派生类和类实例更难。Not exposing a DependencyProperty identifier does not increase the security of your read-only dependency property in any way, it just makes operations that involve your property more awkward both for subsequent derived classes and class instances.

若要在DependencyProperty类中公开标识符, 请直接DependencyProperty对键调用。To expose the DependencyProperty identifier on your class, you call DependencyProperty directly on your key. 使用此值在public static readonly DependencyProperty类上创建一个与相同的标识符。 DependencyPropertyKeyUse this value to create a public static readonlyDependencyProperty identifier on the class, which parallels the DependencyPropertyKey.

适用于

另请参阅