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.

傳回

相依性屬性索引鍵應該用來設定類別中靜態唯讀欄位的值,稍後用於參考相依性屬性。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 會傳回 DependencyProperty的類型。This 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.DependencyProperty 的值公開為類別上的 public 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.

唯讀附加屬性是很罕見的案例,因為附加屬性的主要案例是它在 XAMLXAML中的使用方式。Read-only attached properties are a rare scenario, because the primary scenario for an attached property is its use in XAMLXAML. 如果沒有公用 setter,則不能在 XAMLXAML 語法中設定附加屬性。Without 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

參數

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.

傳回

相依性屬性索引鍵應該用來在您的類別中設定靜態唯獨欄位之值,然後用來參考相依性屬性。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 會傳回 DependencyProperty的類型。This method returns the type DependencyPropertyKey, whereas RegisterAttached returns the type DependencyProperty. 通常,代表類型 DependencyProperty的索引鍵。Typically, 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.DependencyProperty 的值公開為類別上的 public 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.

唯讀附加屬性是很罕見的案例,因為附加屬性的主要案例是它在 XAMLXAML中的使用方式。Read-only attached properties are a rare scenario, because the primary scenario for an attached property is its use in XAMLXAML. 如果沒有公用 setter,則不能在 XAMLXAML 語法中設定附加屬性。Without 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.

適用於