DependencyProperty.RegisterAttached 方法

定义

在属性系统上注册附加属性。Registers an attached property with the property system.

重载

RegisterAttached(String, Type, Type)

使用指定的属性名称、属性类型和所有者类型注册附加属性。Registers an attached property with the specified property name, property type, and owner type.

RegisterAttached(String, Type, Type, PropertyMetadata)

使用指定的属性名、属性类型、所有者类型和属性元数据注册附加的属性。Registers an attached property with the specified property name, property type, owner type, and property metadata.

RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的属性类型、所有者类型、属性元数据和属性的值验证回调来注册附加属性。Registers an attached property with the specified property type, owner type, property metadata, and value validation callback for the property.

RegisterAttached(String, Type, Type)

使用指定的属性名称、属性类型和所有者类型注册附加属性。Registers an attached property with the specified property name, property type, and owner type.

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

参数

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.

返回

一个依赖属性标识符,应使用它来设置类中 public static readonly 字段的值。A dependency property identifier that should be used to set the value of a public static readonly field in your class. 稍后将此标识符用来引用依赖属性,从而实现以编程方式设置其值或获取元数据等操作。That identifier is then used to reference the dependency property later, for operations such as setting its value programmatically or obtaining metadata.

示例

下面的示例使用此 RegisterAttached 签名在抽象类上注册附加属性。The following example registers an attached property on an abstract class using this RegisterAttached signature.

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
  "IsBubbleSource",
  typeof(Boolean),
  typeof(AquariumObject2)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
    element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
    return (Boolean)element.GetValue(IsBubbleSourceProperty);
}
Public Shared ReadOnly IsBubbleSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsBubbleSource", GetType(Boolean), GetType(AquariumObject2))
Public Shared Sub SetIsBubbleSource(ByVal element As UIElement, ByVal value As Boolean)
    element.SetValue(IsBubbleSourceProperty, value)
End Sub
Public Shared Function GetIsBubbleSource(ByVal element As UIElement) As Boolean
    Return CType(element.GetValue(IsBubbleSourceProperty), Boolean)
End Function

注解

附加属性是由 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML)定义的属性概念。An attached property is a property concept defined by 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML). WPFWPF 实现附加属性作为依赖属性。implements attached properties as dependency properties. 由于 WPFWPF 附加属性是依赖属性,因此可以应用元数据,常规属性系统可以使用这些元数据来执行报表布局特征等操作。Because the WPFWPF attached properties are dependency properties, they can have metadata applied that can be used by the general property system for operations such as reporting layout characteristics. 有关详细信息,请参阅附加属性概述For more information, see Attached Properties Overview.

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

另请参阅

RegisterAttached(String, Type, Type, PropertyMetadata)

使用指定的属性名、属性类型、所有者类型和属性元数据注册附加的属性。Registers an attached property with the specified property name, property type, owner type, and property metadata.

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

参数

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. 这可以包括默认值和其他特征。This can include the default value as well as other characteristics.

返回

一个依赖属性标识符,应使用它来设置类中 public static readonly 字段的值。A dependency property identifier that should be used to set the value of a public static readonly field in your class. 稍后将此标识符用来引用依赖属性,从而实现以编程方式设置其值或获取元数据等操作。That identifier is then used to reference the dependency property later, for operations such as setting its value programmatically or obtaining metadata.

注解

附加属性是由 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML)定义的属性概念。An attached property is a property concept defined by 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML). WPFWPF 实现附加属性作为依赖属性。implements attached properties as dependency properties. 由于 WPFWPF 附加属性是依赖属性,因此可以应用元数据,常规属性系统可以使用这些元数据来执行报表布局特征等操作。Because the WPFWPF attached properties are dependency properties, they can have metadata applied that can be used by the general property system for operations such as reporting layout characteristics. 有关详细信息,请参阅附加属性概述For more information, see Attached Properties Overview.

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

使用 System.windows.dependencyproperty.registerattached 进行值继承依赖属性Use RegisterAttached for Value-inheriting Dependency Properties

RegisterAttached 而不是 Register 注册依赖属性的一个特定方案是支持属性值继承。One particular scenario for registering a dependency property with RegisterAttached 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.

另请参阅

RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的属性类型、所有者类型、属性元数据和属性的值验证回调来注册附加属性。Registers an attached property with the specified property type, owner type, property metadata, and value validation callback for the property.

public:
 static System::Windows::DependencyProperty ^ RegisterAttached(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ defaultMetadata, System::Windows::ValidateValueCallback ^ validateValueCallback);
public static System.Windows.DependencyProperty RegisterAttached (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata defaultMetadata, System.Windows.ValidateValueCallback validateValueCallback);
static member RegisterAttached : string * Type * Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyProperty

参数

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. 这可以包括默认值和其他特征。This can include the default value as well as other characteristics.

validateValueCallback
ValidateValueCallback

对回调的引用,除了典型的类型验证之外,该引用还应执行依赖属性值的任何自定义验证。A reference to a callback that should perform any custom validation of the dependency property value beyond typical type validation.

返回

一个依赖属性标识符,应使用它来设置类中 public static readonly 字段的值。A dependency property identifier that should be used to set the value of a public static readonly field in your class. 稍后将此标识符用来引用依赖属性,从而实现以编程方式设置其值或获取元数据等操作。That identifier is then used to reference the dependency property later, for operations such as setting its value programmatically or obtaining metadata.

示例

下面的示例使用此 RegisterAttached 签名在抽象类上注册附加属性。The following example registers an attached property on an abstract class using this RegisterAttached signature. 此附加属性是一个枚举类型属性,注册将添加一个验证回调,以验证提供的值是否为枚举的值。This attached property is an enumeration type property, and the registration adds a validation callback to verify that the provided value is a value of the enumeration.

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
  "IsBubbleSource",
  typeof(Boolean),
  typeof(AquariumObject2)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
    element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
    return (Boolean)element.GetValue(IsBubbleSourceProperty);
}
Public Shared ReadOnly IsBubbleSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsBubbleSource", GetType(Boolean), GetType(AquariumObject2))
Public Shared Sub SetIsBubbleSource(ByVal element As UIElement, ByVal value As Boolean)
    element.SetValue(IsBubbleSourceProperty, value)
End Sub
Public Shared Function GetIsBubbleSource(ByVal element As UIElement) As Boolean
    Return CType(element.GetValue(IsBubbleSourceProperty), Boolean)
End Function

注解

附加属性是由 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML)定义的属性概念。An attached property is a property concept defined by 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML). WPFWPF 实现附加属性作为依赖属性。implements attached properties as dependency properties. 由于 WPFWPF 附加属性是依赖属性,因此可以应用元数据,常规属性系统可以使用这些元数据来执行报表布局特征等操作。Because the WPFWPF attached properties are dependency properties, they can have metadata applied that can be used by the general property system for operations such as reporting layout characteristics. 有关详细信息,请参阅附加属性概述For more information, see Attached Properties Overview.

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

使用 System.windows.dependencyproperty.registerattached 进行值继承依赖属性Use RegisterAttached for Value-inheriting Dependency Properties

RegisterAttached 而不是 Register 注册依赖属性的一个特定方案是支持属性值继承。One particular scenario for registering a dependency property with RegisterAttached 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.

另请参阅

适用于