DependencyProperty.RegisterReadOnly 方法

定義

註冊相依性屬性為唯讀相依性屬性。

多載

RegisterReadOnly(String, Type, Type, PropertyMetadata)

使用指定的屬性類型、擁有者類型和屬性中繼資料登錄唯讀相依性屬性。

RegisterReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼登錄唯讀相依性屬性。

RegisterReadOnly(String, Type, Type, PropertyMetadata)

使用指定的屬性類型、擁有者類型和屬性中繼資料登錄唯讀相依性屬性。

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

參數

name
String

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

propertyType
Type

屬性的類型。

ownerType
Type

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

typeMetadata
PropertyMetadata

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

傳回

DependencyPropertyKey

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

範例

下列範例會將 AquariumSize 相依性屬性註冊為唯讀。 此範例會將 AquariumSizeKey 定義為內部金鑰 (,讓元件中的其他類別可以覆寫中繼資料) ,並根據該索引鍵公開相依性屬性識別碼作為 AquariumSizeProperty 。 此外,系統會針對 AquariumSize 建立包裝函式,且只有 get 存取子。

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

唯讀相依性屬性是現有 API 和自訂案例中相當典型的案例,因為其他 WPF 功能可能需要相依性屬性,即使該屬性不是由呼叫端設定。 您可以使用唯讀相依性屬性的值做為採用相依性屬性的其他屬性系統作業的基礎,例如根據 Trigger 樣式中的相依性屬性。

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

適用於

RegisterReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼登錄唯讀相依性屬性。

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

參數

name
String

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

propertyType
Type

屬性的類型。

ownerType
Type

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

typeMetadata
PropertyMetadata

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

validateValueCallback
ValidateValueCallback

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

傳回

DependencyPropertyKey

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

備註

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

唯讀相依性屬性是相當典型的案例。 您可以使用唯讀相依性屬性的值做為採用相依性屬性的其他屬性系統作業的基礎,例如根據 Trigger 樣式中的相依性屬性。

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

唯讀相依性屬性的驗證可能較不重要。 您為索引鍵指定的非公用存取層級可降低任意無效輸入的可能性。

適用於