DependencyProperty.RegisterAttachedReadOnly 方法

定義

註冊唯讀的附加屬性。

多載

RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata)

註冊具有指定屬性類型、擁有者類型和屬性中繼資料的唯讀附加屬性。

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

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼,註冊一個唯讀附加屬性。

RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata)

註冊具有指定屬性類型、擁有者類型和屬性中繼資料的唯讀附加屬性。

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

要註冊之相依性屬性的名稱。

propertyType
Type

屬性的類型。

ownerType
Type

正在註冊相依性屬性的擁有者類型。

defaultMetadata
PropertyMetadata

相依性屬性的屬性中繼資料。

傳回

DependencyPropertyKey

相依性屬性索引鍵應該用來設定類別中靜態唯讀欄位的值,稍後用於參考相依性屬性。

備註

這個方法會傳回 型別 DependencyPropertyKey ,而 RegisterAttached 會傳回 型別 DependencyProperty 。 通常,代表唯讀屬性的索引鍵不會設為公用,因為索引鍵可用來藉由呼叫 SetValue(DependencyPropertyKey, Object) 來設定相依性屬性值。 您的類別設計會影響您的需求,但通常建議將任何 DependencyPropertyKey 的存取權和可見度限制為僅將相依性屬性設定為類別或應用程式邏輯的一部分所需的程式碼部分。 此外,建議您在類別上將 的值 DependencyPropertyKey.DependencyProperty 公開為 public static readonly 欄位,以公開唯讀相依性屬性的相依性屬性識別碼。

唯讀附加屬性是罕見的案例,因為附加屬性的主要案例是在 XAML 中使用。 如果沒有公用 setter,附加屬性就無法在 XAML 語法中設定。

如需相依性屬性註冊的詳細資訊,請參閱 DependencyProperty

針對值繼承相依性屬性使用 RegisterAttached

將相依性屬性註冊為附加的其中一個特定案例是支援屬性值繼承。 即使 類別定義公開相依性屬性的屬性包裝函式存取子,而且即使您不想要公開 Get* 和 Set* 靜態方法,也應該向 註冊值繼承相依性屬性 RegisterAttached ,以提供真正的附加屬性支援存取子。 雖然屬性值繼承可能適用于非附加的相依性屬性,但未定義執行時間樹狀結構中特定元素界限的非附加屬性繼承行為。 將屬性註冊為附加實際上會使附加屬性成為屬性系統的全域屬性,並確保屬性值繼承可跨專案樹狀結構中的所有界限運作。 一律使用 RegisterAttached 來註冊您在 Inherits 中繼資料中指定的屬性。 如需詳細資訊,請參閱屬性值繼承

適用於

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

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼,註冊一個唯讀附加屬性。

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

要註冊之相依性屬性的名稱。

propertyType
Type

屬性的類型。

ownerType
Type

正在註冊相依性屬性的擁有者類型。

defaultMetadata
PropertyMetadata

相依性屬性的屬性中繼資料。

validateValueCallback
ValidateValueCallback

使用者建立回呼的參考,應該執行對相依性屬性值之一般類型驗證以外的任何自訂驗證。

傳回

DependencyPropertyKey

相依性屬性索引鍵應該用來在您的類別中設定靜態唯獨欄位之值,然後用來參考相依性屬性。

備註

這個方法會傳回 型別 DependencyPropertyKey ,而 RegisterAttached 會傳回 型別 DependencyProperty 。 通常,代表 類型的 DependencyProperty 索引鍵。 通常,代表唯讀屬性的索引鍵不會設為公用,因為索引鍵可用來藉由呼叫 SetValue(DependencyPropertyKey, Object) 來設定相依性屬性值。 您的類別設計會影響您的需求,但通常建議將任何 DependencyPropertyKey 的存取權和可見度限制為僅將相依性屬性設定為類別或應用程式邏輯的一部分所需的程式碼部分。 此外,建議您在類別上將 的值 DependencyPropertyKey.DependencyProperty 公開為 public static readonly 欄位,以公開唯讀相依性屬性的相依性屬性識別碼。

唯讀附加屬性是罕見的案例,因為附加屬性的主要案例是在 XAML 中使用。 如果沒有公用 setter,附加屬性就無法在 XAML 語法中設定。

如需相依性屬性註冊的詳細資訊,請參閱 DependencyProperty

針對值繼承相依性屬性使用 RegisterAttached

將相依性屬性註冊為附加,而不是 Register 的其中一個特定案例是支援屬性值繼承。 即使 類別定義公開相依性屬性的屬性包裝函式存取子,而且即使您不想要公開 Get* 和 Set* 靜態方法,也應該向 註冊值繼承相依性屬性 RegisterAttached ,以提供真正的附加屬性支援存取子。 雖然屬性值繼承可能適用于非附加的相依性屬性,但未定義執行時間樹狀結構中特定元素界限的非附加屬性繼承行為。 將屬性註冊為附加實際上會使附加屬性成為屬性系統的全域屬性,並確保屬性值繼承可跨專案樹狀結構中的所有界限運作。 一律使用 RegisterAttached 來註冊您在 Inherits 中繼資料中指定的屬性。 如需詳細資訊,請參閱屬性值繼承

適用於