DependencyPropertyKey Class


為唯讀相依性屬性的有限寫入權限提供相依性屬性識別項。Provides a dependency property identifier for limited write access to a read-only dependency property.

public ref class DependencyPropertyKey sealed
public sealed class DependencyPropertyKey
type DependencyPropertyKey = class
Public NotInheritable Class DependencyPropertyKey


下列範例會註冊唯讀相依性屬性,也會在其他類別成員中使用索引鍵做為兩個用途:執行 get 「包裝函式」,以及做為受保護的判斷作業的識別碼,以根據其他屬性值的計算來設定值。The following example registers a read-only dependency property, and also uses the key for two purposes in other class members: implementing the get "wrapper", and as the identifier for a protected determination operation that sets the value based on calculations of other property values.

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


DependencyPropertyKey 實例會使用 RegisterReadOnlyRegisterAttachedReadOnly的方法,取得為相依性屬性註冊呼叫的傳回值。DependencyPropertyKey instances are obtained as the return value of a dependency property registration call using the methods RegisterReadOnly or RegisterAttachedReadOnly.

註冊相依性屬性的類型可以使用 DependencyPropertyKey 呼叫中的 SetValueClearValue,以將屬性的值調整為類別邏輯的一部分。The types that register a dependency property can use the DependencyPropertyKey in calls to SetValue and ClearValue that adjust the property's value as part of class logic. 如果金鑰的存取層級允許,相關類別也可以使用索引鍵和相依性屬性。If permitted by the access level of the key, related classes can use the key and the dependency property also. 例如,您可以將索引鍵宣告為 internal,而相同元件內的其他類型也可以設定該相依性屬性。For instance, you can declare the key as internal, and other types within the same assembly can also set that dependency property.

唯讀相依性屬性註冊所傳回的 DependencyPropertyKey 不應設為公用,因為公開索引鍵可設定屬性,因此會達到監控將它註冊為唯讀相依性屬性的點。The DependencyPropertyKey returned by read-only dependency property registration should not be made public, because exposing the key makes the property settable, thus defeating the point of registering it as a read-only dependency property. 此外,公開索引鍵會導致可用的相依性屬性行為和其 通用語言執行平台 (CLR)common language runtime (CLR) 屬性包裝函式(這是不正確的類別設計)之間出現不相符的情形。Also, exposing the key causes a mismatch between the available dependency property behaviors and its 通用語言執行平台 (CLR)common language runtime (CLR) property wrapper implementations, which is bad class design.

您應該改為將 DependencyPropertyKeyDependencyProperty 值公開為類別上的 public static readonlyDependencyProperty,而不是公開金鑰本身。Instead of exposing the key itself, you should instead expose the DependencyProperty value of the DependencyPropertyKey as a public static readonlyDependencyProperty on your class. 這可讓屬性針對特定的屬性系統作業(例如列舉本機設定的值)傳回有效的相依性屬性識別碼。This enables the property to return a valid dependency property identifier for certain property system operations such as enumerating locally set values. 不過,取得的識別碼對於許多屬性系統作業並沒有 DependencyProperty 的完整功能。However, the identifier thus obtained does not have the full capabilities of a DependencyProperty for many property system operations.



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



判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
OverrideMetadata(Type, PropertyMetadata)

覆寫由相依性屬性識別項表示之唯讀相依性屬性的中繼資料。Overrides the metadata of a read-only dependency property that is represented by this dependency property identifier.


傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also