DependencyPropertyKey.DependencyProperty 屬性


取得與這個特定的相依性屬性識別項關聯的相依性屬性識別項。Gets the dependency property identifier associated with this specialized read-only dependency property identifier.

 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.


下列範例會呼叫DependencyProperty , 以公開DependencyProperty類別上AquariumGraphicProperty AquariumGraphic唯讀相依性屬性的識別碼 ()。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(
  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


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上呼叫, 並取得本機 set 屬性 (值和識別碼) 的列舉, 則針對唯讀相依性屬性傳回的識別碼會是您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 DependencyPropertyKey類別上建立DependencyProperty與相同的識別碼。Use this value to create a public static readonlyDependencyProperty identifier on the class, which parallels the DependencyPropertyKey.