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 內部成員) ,以及 的 AquariumGraphic 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

備註

DependencyProperty 可讓唯讀屬性的識別碼使用一些用於讀寫相依性屬性的相同介面參與通用屬性系統作業。

若要實作唯讀相依性屬性的 get 屬性存取子,您應該在類別上建立和公開 DependencyProperty 識別碼。 這有兩個目的:

  • 您自己的類別需要 DependencyProperty 識別碼,才能實作屬性包裝函式的 get 存取子。 DependencyProperty您可以使用 做為 GetValue 實作 get 存取子之呼叫的參數。

  • DependencyProperty 識別碼會將相依性屬性公開至屬性系統,讓依賴中繼資料的其他方法可以在標準形式中存取它。 例如,如果您在部分 DependencyObject 上呼叫 GetLocalValueEnumerator 並取得本機設定屬性的列舉, (值和識別碼) 針對唯讀相依性屬性傳回的識別碼會是您的 DependencyProperty 值,而不是索引鍵。 不公開 DependencyProperty 識別碼並不會以任何方式增加唯讀相依性屬性的安全性,它只會讓涉及您屬性的作業對後續衍生類別和類別實例而言更為不方便。

若要在您的類別上公開 DependencyProperty 識別碼,您可以直接在金鑰上呼叫 DependencyProperty 。 使用此值在類別上建立 public static readonlyDependencyProperty 識別碼,以平行處理 DependencyPropertyKey

適用於

另請參閱