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. 如果没有公共资源库,则不能在 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. 即使类定义公开依赖项属性的属性包装访问器,甚至不打算公开 Get * 和 Set * 静态方法以提供 true 附加属性,您也应该用 RegisterAttached 注册值继承依赖属性。属性支持访问器。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. 如果没有公共资源库,则不能在 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. 即使类定义公开依赖项属性的属性包装访问器,甚至不打算公开 Get * 和 Set * 静态方法以提供 true 附加属性,您也应该用 RegisterAttached 注册值继承依赖属性。属性支持访问器。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.

适用于