DependencyPropertyKey.DependencyProperty 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得與這個特定的相依性屬性識別項關聯的相依性屬性識別項。
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 readonly
DependencyProperty 識別碼,以平行處理 DependencyPropertyKey 。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應