語言

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。

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 property accessor,你應該在類別上建立並公開一個 DependencyProperty 識別碼。 這有兩個用途:

  • 你自己的類別需要識別 DependencyProperty 碼來實作屬性包裝器的 get accessor。 你用 作為 DependencyProperty 呼叫的參數,來實 GetValue 作 get accessor。

  • DependencyProperty 識別碼會將你的相依屬性暴露給屬性系統,使其他依賴元資料的方法能以標準形式存取。 舉例來說,如果你呼叫 GetLocalValueEnumerator 某個 DependencyObject 屬性並取得本地設定屬性(值和識別碼)的列舉,對於只讀依賴屬性回傳的識別碼,會是你的 DependencyProperty 值,而不是鍵值。 不公開 DependencyProperty 識別碼並不會提升唯讀相依屬性的安全性,只會讓涉及你屬性的操作對後續衍生類別和類別實例來說變得更尷尬。

要公開你的類別識別碼 DependencyProperty ,你必須直接呼叫 DependencyProperty 你的金鑰。 利用這個值在類別上建立 public static readonlyDependencyProperty 一個識別碼,與 DependencyPropertyKey.

適用於

另請參閱