DependencyPropertyKey 類別

定義

為唯讀相依性屬性的有限寫入權限提供相依性屬性識別項。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
繼承
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(
  "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

備註

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

註冊相依性屬性的型別可以DependencyPropertyKey在對的SetValue呼叫中使用ClearValue , 並將屬性的值調整為類別邏輯的一部分。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.

DependencyProperty應該改DependencyPropertyKey為將的值公開為類別上的public static readonly DependencyProperty , 而不是公開索引鍵本身。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.

屬性

DependencyProperty

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

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

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

(繼承來源 Object)
GetType()

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

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
OverrideMetadata(Type, PropertyMetadata)

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

ToString()

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

(繼承來源 Object)

適用於

另請參閱