DependencyProperty DependencyProperty DependencyProperty DependencyProperty Class

定义

表示可通过诸如样式、数据绑定、动画和继承等方法设置的属性。Represents a property that can be set through methods such as, styling, data binding, animation, and inheritance.

public ref class DependencyProperty sealed
[System.ComponentModel.TypeConverter("System.Windows.Markup.DependencyPropertyConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
[System.ComponentModel.TypeConverter("System.Windows.Markup.DependencyPropertyConverter, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
public sealed class DependencyProperty
type DependencyProperty = class
Public NotInheritable Class DependencyProperty
继承
DependencyPropertyDependencyPropertyDependencyPropertyDependencyProperty
属性

注解

DependencyProperty支持中Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)的以下功能:A DependencyProperty supports the following capabilities in Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF):

  • 可以在样式中设置属性。The property can be set in a style. 有关详细信息,请参阅样式设置和模板化For more information, see Styling and Templating.

  • 可以通过数据绑定设置属性。The property can be set through data binding. 有关数据绑定依赖项属性的详细信息, 请参阅如何:绑定两个控件的属性。For more information about data binding dependency properties, see How to: Bind the Properties of Two Controls.

  • 可以通过动态资源引用来设置属性。The property can be set with a dynamic resource reference. 有关详细信息,请参阅 XAML 资源For more information, see XAML Resources.

  • 属性可以从元素树中的父元素自动继承其值。The property can inherit its value automatically from a parent element in the element tree. 有关详细信息,请参阅属性值继承For more information, see Property Value Inheritance.

  • 可以对属性进行动画处理。The property can be animated. 有关详细信息,请参阅 动画概述For more information, see Animation Overview.

  • 属性的上一个值已更改并且属性值可以被强制时, 属性可报告。The property can report when the previous value of the property has been changed and the property value can be coerced. 有关详细信息,请参阅依赖属性回调和验证For more information, see Dependency Property Callbacks and Validation.

  • 属性将信息报告给WPFWPF, 例如更改属性值是否应要求布局系统 recompose 元素的视觉对象。The property reports information to WPFWPF, such as whether changing a property value should require the layout system to recompose the visuals for an element.

  • 属性在中适用于 Visual Studio 的 WPF 设计器WPF Designer for Visual Studio接收支持。The property receives support in the 适用于 Visual Studio 的 WPF 设计器WPF Designer for Visual Studio. 例如, 可以在 "属性" 窗口中编辑该属性。For example, the property can be edited in the Properties window.

若要了解有关依赖项属性的详细信息, 请参阅依赖属性概述To learn more about dependency properties, see Dependency Properties Overview. 如果希望自定义类型的属性支持上述列表中的功能, 则应创建一个依赖项属性。If you want properties on your custom types to support the capabilities in the preceding list, you should create a dependency property. 若要了解如何创建自定义依赖项属性, 请参阅自定义依赖属性To learn how to create custom dependency properties, see Custom Dependency Properties.

附加属性是一种属性, 该属性使任何对象都可以向定义附加属性的类型报告信息。An attached property is a property that enables any object to report information to the type that defines the attached property. WPFWPF中, 从DependencyObject继承的任何类型都可以使用附加属性, 无论该类型是否继承自定义该属性的类型。In WPFWPF, any type that inherits from DependencyObject can use an attached property regardless of whether the type inherits from the type that defines the property. 附加属性是XAMLXAML语言的一项功能。An attached property is a feature of the XAMLXAML language. 若要在中XAMLXAML设置附加属性, 请使用所有者的。propertyName语法。To set an attached property in XAMLXAML, use the ownerType.propertyName syntax. 附加属性的一个示例是DockPanel.Dock属性。An example of an attached property is the DockPanel.Dock property. 如果要创建可用于所有DependencyObject类型的属性, 则应创建附加属性。If you want to create a property that can be used on all DependencyObject types, then you should create an attached property. 若要了解有关附加属性的详细信息 (包括如何创建附加属性), 请参阅附加属性概述To learn more about attached properties, including how to create them, see Attached Properties Overview.

XAML 属性用法XAML Attribute Usage

<object property="dependencyPropertyName"/>  

-or-

<object property="ownerType.dependencyPropertyName"/>  

-or-

<object property="attachedPropertyOwnerType.attachedPropertyName"/>  

XAML 值XAML Values

dependencyPropertyName
一个字符串, 指定DependencyProperty.Name所需依赖属性的。A string that specifies the DependencyProperty.Name of the desired dependency property. 如果该属性不在默认的 XML 命名空间中, 则可以在其前面加上一个 XML 命名空间前缀 (有关详细信息, 请参阅WPF xaml 的 Xaml 命名空间和命名空间映射)。This can be preceded by an XML namespace prefix if the property is not in the default XML namespace (for details, see XAML Namespaces and Namespace Mapping for WPF XAML.)

ownerType.dependencyPropertyNameownerType.dependencyPropertyName
一个字符串, 该字符串指定依赖项属性的所有者类型、点 (.)、和DependencyProperty.NameA string that specifies an owner type of a dependency property, a dot (.), then the DependencyProperty.Name. ownerType还可以以 XML 命名空间前缀开头。ownerType can also be preceded by an XML namespace prefix. 此用法特别适用于晚期绑定样式和模板, 其中必须指定依赖属性的所有者来分析上下文, 因为TargetType尚不知道。This usage is particular to late-bound styles and templates, where the owner of the dependency property must be specified for parsing context because the TargetType is not yet known. 有关详细信息,请参阅样式设置和模板化For more information, see Styling and Templating.

attachedPropertyOwnerType 的哈希代码。attachedPropertyOwnerType . attachedPropertyName
一个字符串, 指定附加属性的所有者、点 (.) 和附加属性名称。A string that specifies the owner of an attached property, a dot (.), then the attached property name. attachedPropertyOwnerType还可以以 XML 命名空间前缀开头。attachedPropertyOwnerType can also be preceded by an XML namespace prefix.

字段

UnsetValue UnsetValue UnsetValue UnsetValue

指定由 WPFWPF 属性系统使用的静态值而非 null,以指示属性存在,但其值未经属性系统设置。Specifies a static value that is used by the WPFWPF property system rather than null to indicate that the property exists, but does not have its value set by the property system.

属性

DefaultMetadata DefaultMetadata DefaultMetadata DefaultMetadata

获取依赖项对象的默认元数据。Gets the default metadata of the dependency property.

GlobalIndex GlobalIndex GlobalIndex GlobalIndex

获取唯一标识依赖项对象的内部生成值。Gets an internally generated value that uniquely identifies the dependency property.

Name Name Name Name

获取依赖属性的名称。Gets the name of the dependency property.

OwnerType OwnerType OwnerType OwnerType

获取使用属性系统注册依赖属性或者将自己作为属性所有者添加的对象的类型。Gets the type of the object that registered the dependency property with the property system, or added itself as owner of the property.

PropertyType PropertyType PropertyType PropertyType

获取依赖项对象用于其值的类型。Gets the type that the dependency property uses for its value.

ReadOnly ReadOnly ReadOnly ReadOnly

获取一个值,该值指示由此 DependencyProperty 实例标识的依赖项对象是否为只读依赖项对象。Gets a value that indicates whether the dependency property identified by this DependencyProperty instance is a read-only dependency property.

ValidateValueCallback ValidateValueCallback ValidateValueCallback ValidateValueCallback

获取依赖项对象的值验证回调。Gets the value validation callback for the dependency property.

方法

AddOwner(Type) AddOwner(Type) AddOwner(Type) AddOwner(Type)

将另一种类型添加为已注册的依赖属性的所有者。Adds another type as an owner of a dependency property that has already been registered.

AddOwner(Type, PropertyMetadata) AddOwner(Type, PropertyMetadata) AddOwner(Type, PropertyMetadata) AddOwner(Type, PropertyMetadata)

将其他类型添加为已注册的依赖对象所有者,从而为依赖属性提供依赖属性元数据使其存在于提供的所有者类型上。Adds another type as an owner of a dependency property that has already been registered, providing dependency property metadata for the dependency property as it will exist on the provided owner type.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此 DependencyProperty 的哈希代码。Returns a hash code for this DependencyProperty.

GetMetadata(DependencyObject) GetMetadata(DependencyObject) GetMetadata(DependencyObject) GetMetadata(DependencyObject)

返回此依赖属性的元数据,因为它存在于指定的对象实例上。Returns the metadata for this dependency property as it exists on the specified object instance.

GetMetadata(DependencyObjectType) GetMetadata(DependencyObjectType) GetMetadata(DependencyObjectType) GetMetadata(DependencyObjectType)

为此依赖属性(当它位于指定的对象实例上时)返回元数据。Returns the metadata for this dependency property as it exists on a specified type.

GetMetadata(Type) GetMetadata(Type) GetMetadata(Type) GetMetadata(Type)

返回此依赖属性的元数据,因为它存在于指定的现有类型上。Returns the metadata for this dependency property as it exists on a specified existing type.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsValidType(Object) IsValidType(Object) IsValidType(Object) IsValidType(Object)

确定指定的值对于该依赖项对象的类型是否可接受(与原依赖项对象注册中提供的属性类型相对照)。Determines whether a specified value is acceptable for this dependency property's type, as checked against the property type provided in the original dependency property registration.

IsValidValue(Object) IsValidValue(Object) IsValidValue(Object) IsValidValue(Object)

确定所提供的值通过基本类型检查后是否被属性类型接受,以及它是否有可能在该类型的值的允许范围以内。Determines whether the provided value is accepted for the type of property through basic type checking, and also potentially if it is within the allowed range of values for that type.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OverrideMetadata(Type, PropertyMetadata) OverrideMetadata(Type, PropertyMetadata) OverrideMetadata(Type, PropertyMetadata) OverrideMetadata(Type, PropertyMetadata)

当此依赖属性位于指定类型的实例上时为其指定替换元数据,以在该依赖属性继承自基类型时重写该属性已存在的元数据。Specifies alternate metadata for this dependency property when it is present on instances of a specified type, overriding the metadata that existed for the dependency property as it was inherited from base types.

OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey) OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey) OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey) OverrideMetadata(Type, PropertyMetadata, DependencyPropertyKey)

当只读依赖属性位于指定类型的实例上时为其提供替换元数据,以便重写在最初注册依赖属性时提供的元数据。Supplies alternate metadata for a read-only dependency property when it is present on instances of a specified type, overriding the metadata that was provided in the initial dependency property registration. 你必须为只读依赖属性传递 DependencyPropertyKey,以免引发异常。You must pass the DependencyPropertyKey for the read-only dependency property to avoid raising an exception.

Register(String, Type, Type) Register(String, Type, Type) Register(String, Type, Type) Register(String, Type, Type)

使用指定的属性名称、属性类型和所有者类型注册依赖属性。Registers a dependency property with the specified property name, property type, and owner type.

Register(String, Type, Type, PropertyMetadata) Register(String, Type, Type, PropertyMetadata) Register(String, Type, Type, PropertyMetadata) Register(String, Type, Type, PropertyMetadata)

使用指定的属性名称、属性类型、所有者类型和属性元数据注册依赖属性。Registers a dependency property with the specified property name, property type, owner type, and property metadata.

Register(String, Type, Type, PropertyMetadata, ValidateValueCallback) Register(String, Type, Type, PropertyMetadata, ValidateValueCallback) Register(String, Type, Type, PropertyMetadata, ValidateValueCallback) Register(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的属性名称、属性类型、所有者类型、属性元数据和属性的值验证回叫来注册依赖属性。Registers a dependency property with the specified property name, property type, owner type, property metadata, and a value validation callback for the property.

RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type)

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

RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata) 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) RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback) RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback) 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.

RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata) RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata) RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata) 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) RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback) RegisterAttachedReadOnly(String, Type, Type, PropertyMetadata, ValidateValueCallback) 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.

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

ToString() ToString() ToString() ToString()

返回依赖属性的字符串表示形式。Returns the string representation of the dependency property.

适用于

另请参阅