DependencyProperty.RegisterAttachedReadOnly 方法

定義

註冊唯讀的附加屬性。Registers a read-only attached property.

多載

RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata)

註冊具有指定屬性類型、擁有者類型和屬性中繼資料的唯讀附加屬性。Registers a read-only attached property, with the specified property type, owner type, and property metadata.

RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼,註冊一個唯讀附加屬性。Registers a read-only attached property, with the specified property type, owner type, property metadata, and a validation callback.

RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata)

註冊具有指定屬性類型、擁有者類型和屬性中繼資料的唯讀附加屬性。Registers a read-only attached property, with the specified property type, owner type, and property metadata.

public:
 static System::Windows::DependencyPropertyKey ^ RegisterAttachedReadOnly(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ defaultMetadata);
public static System.Windows.DependencyPropertyKey RegisterAttachedReadOnly (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata defaultMetadata);
static member RegisterAttachedReadOnly : string * Type * Type * System.Windows.PropertyMetadata -> System.Windows.DependencyPropertyKey
Public Shared Function RegisterAttachedReadOnly (name As String, propertyType As Type, ownerType As Type, defaultMetadata As PropertyMetadata) As DependencyPropertyKey

參數

name
String

要註冊之相依性屬性的名稱。The name of the dependency property to register.

propertyType
Type

屬性的類型。The type of the property.

ownerType
Type

正在註冊相依性屬性的擁有者類型。The owner type that is registering the dependency property.

defaultMetadata
PropertyMetadata

相依性屬性的屬性中繼資料。Property metadata for the dependency property.

傳回

DependencyPropertyKey

相依性屬性索引鍵應該用來設定類別中靜態唯讀欄位的值,稍後用於參考相依性屬性。A dependency property key that should be used to set the value of a static read-only field in your class, which is then used to reference the dependency property later.

備註

這個方法會傳回型別 DependencyPropertyKey ,而會傳回 RegisterAttached 型別 DependencyPropertyThis method returns the type DependencyPropertyKey, whereas RegisterAttached returns the type DependencyProperty. 通常表示唯讀屬性的索引鍵並不會被公開,因為可以透過呼叫來使用這些索引鍵來設定相依性屬性值 SetValue(DependencyPropertyKey, Object)Typically, the keys that represent read-only properties are not made public, because the keys can be used to set the dependency property value by calling SetValue(DependencyPropertyKey, Object). 您的類別設計將會影響您的需求,但通常建議您將任何程式碼部分的存取和可見度限制為 DependencyPropertyKey 只需要將該相依性屬性設定為類別或應用程式邏輯的一部分。Your class design will affect your requirements, but it is generally recommended to limit the access and visibility of any DependencyPropertyKey to only those parts of your code that are necessary to set that dependency property as part of class or application logic. 也建議您公開唯讀相依性屬性的相依性屬性識別碼,方法是將的值公開 DependencyPropertyKey.DependencyPropertypublic static readonly 您類別的欄位。It is also recommended that you expose a dependency property identifier for the read-only dependency property, by exposing the value of DependencyPropertyKey.DependencyProperty as a public static readonly field on your class.

唯讀附加屬性是很罕見的案例,因為附加屬性的主要案例是在中使用 XAMLXAMLRead-only attached properties are a rare scenario, because the primary scenario for an attached property is its use in XAMLXAML. 如果沒有公用 setter,則無法在語法中設定附加的屬性 XAMLXAMLWithout a public setter, an attached property cannot be set in XAMLXAML syntax.

如需相依性屬性註冊的詳細資訊,請參閱 DependencyPropertyFor more information on dependency property registration, see DependencyProperty.

使用 System.windows.dependencyproperty.registerattached 進行值繼承相依性屬性Use RegisterAttached for Value-inheriting Dependency Properties

將相依性屬性註冊為附加的一個特定案例,是為了支援屬性值繼承。One particular scenario for registering a dependency property as attached is to support property value inheritance. 您應該登錄值繼承的相依性屬性 RegisterAttached ,即使類別定義了公開相依性屬性的屬性包裝存取子,即使您不打算公開 Get * 和 Set * 靜態方法,也能提供真正的附加屬性支援存取子。You should register value-inheriting dependency properties with RegisterAttached even if the class defines property wrapper accessors that expose the dependency property, and even if you do not intend to expose Get* and Set* static methods to provide true attached property support accessors. 雖然非附加的相依性屬性可能會出現屬性值繼承,但是透過執行時間樹狀結構中特定專案界限的非附加屬性繼承行為是未定義的。Although property value inheritance might appear to work for nonattached dependency properties, the inheritance behavior for a nonattached property through certain element boundaries in the runtime tree is undefined. 將屬性註冊為附加屬性可有效地將附加屬性設為屬性系統的全域屬性,並確保屬性值繼承可以跨元素樹狀結構中的所有界限運作。Registering the property as attached effectively makes the attached property a global property to the property system, and assures that property value inheritance works across all boundaries in an element tree. 一律使用 RegisterAttached 註冊您 Inherits 在中繼資料中指定的屬性。Always use RegisterAttached to register properties where you specify Inherits in the metadata. 如需詳細資訊,請參閱屬性值繼承For more information, see Property Value Inheritance.

適用於

RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼,註冊一個唯讀附加屬性。Registers a read-only attached property, with the specified property type, owner type, property metadata, and a validation callback.

public:
 static System::Windows::DependencyPropertyKey ^ RegisterAttachedReadOnly(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ defaultMetadata, System::Windows::ValidateValueCallback ^ validateValueCallback);
public static System.Windows.DependencyPropertyKey RegisterAttachedReadOnly (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata defaultMetadata, System.Windows.ValidateValueCallback validateValueCallback);
static member RegisterAttachedReadOnly : string * Type * Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyPropertyKey
Public Shared Function RegisterAttachedReadOnly (name As String, propertyType As Type, ownerType As Type, defaultMetadata As PropertyMetadata, validateValueCallback As ValidateValueCallback) As DependencyPropertyKey

參數

name
String

要註冊之相依性屬性的名稱。The name of the dependency property to register.

propertyType
Type

屬性的類型。The type of the property.

ownerType
Type

正在註冊相依性屬性的擁有者類型。The owner type that is registering the dependency property.

defaultMetadata
PropertyMetadata

相依性屬性的屬性中繼資料。Property metadata for the dependency property.

validateValueCallback
ValidateValueCallback

使用者建立回呼的參考,應該執行對相依性屬性值之一般類型驗證以外的任何自訂驗證。A reference to a user-created callback that should perform any custom validation of the dependency property value beyond typical type validation.

傳回

DependencyPropertyKey

相依性屬性索引鍵應該用來在您的類別中設定靜態唯獨欄位之值,然後用來參考相依性屬性。A dependency property key that should be used to set the value of a static read-only field in your class, which is then used to reference the dependency property.

備註

這個方法會傳回型別 DependencyPropertyKey ,而會傳回 RegisterAttached 型別 DependencyPropertyThis method returns the type DependencyPropertyKey, whereas RegisterAttached returns the type DependencyProperty. 通常是代表類型的索引鍵 DependencyPropertyTypically, the keys that represent the type DependencyProperty. 通常表示唯讀屬性的索引鍵並不會被公開,因為可以透過呼叫來使用這些索引鍵來設定相依性屬性值 SetValue(DependencyPropertyKey, Object)Typically, the keys that represent read-only properties are not made public, because the keys can be used to set the dependency property value by calling SetValue(DependencyPropertyKey, Object). 您的類別設計將會影響您的需求,但通常建議您將任何程式碼部分的存取和可見度限制為 DependencyPropertyKey 只需要將該相依性屬性設定為類別或應用程式邏輯的一部分。Your class design will affect your requirements, but it is generally recommended to limit the access and visibility of any DependencyPropertyKey to only those parts of your code that are necessary to set that dependency property as part of class or application logic. 也建議您公開唯讀相依性屬性的相依性屬性識別碼,方法是將的值公開 DependencyPropertyKey.DependencyPropertypublic static readonly 您類別的欄位。It is also recommended that you expose a dependency property identifier for the read-only dependency property, by exposing the value of DependencyPropertyKey.DependencyProperty as a public static readonly field on your class.

唯讀附加屬性是很罕見的案例,因為附加屬性的主要案例是在中使用 XAMLXAMLRead-only attached properties are a rare scenario, because the primary scenario for an attached property is its use in XAMLXAML. 如果沒有公用 setter,則無法在語法中設定附加的屬性 XAMLXAMLWithout a public setter, an attached property cannot be set in XAMLXAML syntax.

如需相依性屬性註冊的詳細資訊,請參閱 DependencyPropertyFor more information on dependency property registration, see DependencyProperty.

使用 System.windows.dependencyproperty.registerattached 進行值繼承相依性屬性Use RegisterAttached for Value-inheriting Dependency Properties

將相依性屬性註冊為附加而非的一個特定案例, Register 是為了支援屬性值繼承。One particular scenario for registering a dependency property as attached instead of Register is to support property value inheritance. 您應該登錄值繼承的相依性屬性 RegisterAttached ,即使類別定義了公開相依性屬性的屬性包裝存取子,即使您不打算公開 Get * 和 Set * 靜態方法,也能提供真正的附加屬性支援存取子。You should register value-inheriting dependency properties with RegisterAttached even if the class defines property wrapper accessors that expose the dependency property, and even if you do not intend to expose Get* and Set* static methods to provide true attached property support accessors. 雖然非附加的相依性屬性可能會出現屬性值繼承,但是透過執行時間樹狀結構中特定專案界限的非附加屬性繼承行為是未定義的。Although property value inheritance might appear to work for nonattached dependency properties, the inheritance behavior for a nonattached property through certain element boundaries in the runtime tree is undefined. 將屬性註冊為附加屬性可有效地將附加屬性設為屬性系統的全域屬性,並確保屬性值繼承可以跨元素樹狀結構中的所有界限運作。Registering the property as attached effectively makes the attached property a global property to the property system, and assures that property value inheritance works across all boundaries in an element tree. 一律使用 RegisterAttached 註冊您 Inherits 在中繼資料中指定的屬性。Always use RegisterAttached to register properties where you specify Inherits in the metadata. 如需詳細資訊,請參閱屬性值繼承For more information, see Property Value Inheritance.

適用於