DependencyProperty.RegisterReadOnly 方法

定义

将依赖属性注册为只读依赖属性。Registers a dependency property as a read-only dependency property.

重载

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)

使用指定的属性类型、所有者类型、属性元数据和验证回叫来注册只读依赖属性。Registers a read-only dependency property, with the specified property type, owner type, property metadata, and a validation callback.

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

要注册的依赖属性的名称。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.

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

只读依赖属性是现有 API 和自定义方案中的相当典型的方案,因为其他 WPFWPF 功能可能需要依赖属性,即使该属性不是由调用方设置的。Read-only dependency properties are a fairly typical scenario both in the existing API 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. 您可以使用只读依赖属性的值作为采用依赖属性的其他属性系统操作的基础,例如以样式中的依赖属性为基础 TriggerYou 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.

有关依赖属性注册的详细信息,请参阅 DependencyPropertyFor more information on dependency property registration, see DependencyProperty.

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

要注册的依赖属性的名称。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.

typeMetadata
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 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. 您可以使用只读依赖属性的值作为采用依赖属性的其他属性系统操作的基础,例如以样式中的依赖属性为基础 TriggerYou 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.

有关依赖属性注册的详细信息,请参阅 DependencyPropertyFor 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.

适用于