DependencyProperty.RegisterReadOnly DependencyProperty.RegisterReadOnly DependencyProperty.RegisterReadOnly DependencyProperty.RegisterReadOnly Method

定義

註冊相依性屬性為唯讀相依性屬性。Registers a dependency property as a read-only dependency property.

多載

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

使用指定的屬性類型、擁有者類型和屬性中繼資料登錄唯讀相依性屬性。Registers a read-only dependency property, with the specified property type, owner type, and property metadata.

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

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼登錄唯讀相依性屬性。Registers a read-only dependency property, with the specified property type, owner type, property metadata, and a validation callback.

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

使用指定的屬性類型、擁有者類型和屬性中繼資料登錄唯讀相依性屬性。Registers a read-only dependency property, with the specified property type, owner type, and property metadata.

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 String String String

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

propertyType
Type Type Type Type

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

ownerType
Type Type Type Type

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

typeMetadata
PropertyMetadata PropertyMetadata PropertyMetadata 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.

範例

下列範例會將相依AquariumSize性屬性註冊為唯讀。The following example registers an AquariumSize dependency property as read-only. 這個範例會AquariumSizeKey將定義為內部索引鍵 (如此一來, 元件中的其他類別可以覆寫中繼資料), 並根據該索引AquariumSizeProperty鍵, 將相依性屬性識別碼公開為。The example defines AquariumSizeKey as an internal key (so that other classes in the assembly could override metadata) and exposes the dependency property identifier based on that key as AquariumSizeProperty. 此外, 也會針對AquariumSize建立包裝函式, 但只有 get 存取子。Also, a wrapper is created for AquariumSize, with only a get accessor.

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傳回型別。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.

唯讀相依性屬性在現有APIAPI和自訂的案例中都是相當常見的案例, 因為其他WPFWPF功能可能需要相依性屬性, 即使該屬性不打算由設定上部.Read-only dependency properties are a fairly typical scenario both in the existing APIAPI and for customization scenarios, because other WPFWPF features might require a dependency property even if that property is not intended to be settable by callers. 您可以使用唯讀相依性屬性的值, 做為其他採用相依性屬性之屬性系統作業的基礎, 例如Trigger , 以樣式中的相依性屬性為基礎。You can use the value of a read-only dependency property as the basis for other property system operations that take a dependency property, such as basing a Trigger on the dependency property in a style.

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

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

使用指定的屬性類型、擁有者類型、屬性中繼資料和驗證回呼登錄唯讀相依性屬性。Registers a read-only dependency property, with the specified property type, owner type, property metadata, and a validation callback.

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

參數

name
String String String String

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

propertyType
Type Type Type Type

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

ownerType
Type Type Type Type

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

typeMetadata
PropertyMetadata PropertyMetadata PropertyMetadata PropertyMetadata

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

validateValueCallback
ValidateValueCallback ValidateValueCallback 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 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.

唯讀相依性屬性是相當常見的案例。Read-only dependency properties are a fairly typical scenario. 您可以使用唯讀相依性屬性的值, 做為其他採用相依性屬性之屬性系統作業的基礎, 例如Trigger , 以樣式中的相依性屬性為基礎。You can use the value of a read-only dependency property as the basis for other property system operations that take a dependency property, such as basing a Trigger on the dependency property in a style.

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

唯讀相依性屬性的驗證可能比較不重要。Validation on a read-only dependency property might be less important. 您為金鑰指定的非公用存取層級可減少任意無效輸入的可能性。The nonpublic access level you specify for the key reduces the likelihood for arbitrary invalid input.

適用於