DependencyObject 类


表示参与依赖属性系统的对象。Represents an object that participates in the dependency property system.

public ref class DependencyObject : System::Windows::Threading::DispatcherObject
[System.Windows.Markup.NameScopeProperty("NameScope", typeof(System.Windows.NameScope))]
public class DependencyObject : System.Windows.Threading.DispatcherObject
public class DependencyObject : System.Windows.Threading.DispatcherObject
[<System.Windows.Markup.NameScopeProperty("NameScope", typeof(System.Windows.NameScope))>]
type DependencyObject = class
    inherit DispatcherObject
type DependencyObject = class
    inherit DispatcherObject
Public Class DependencyObject
Inherits DispatcherObject


下面的示例从派生 DependencyObject ,以创建一个新的抽象类。The following example derives from DependencyObject to create a new abstract class. 然后,类将注册附加属性,并包含该附加属性的支持成员。The class then registers an attached property and includes support members for that attached property.

public abstract class AquariumObject3 : DependencyObject
    public enum Bouyancy
    public static readonly DependencyProperty BouyancyProperty = DependencyProperty.RegisterAttached(
      new FrameworkPropertyMetadata(Bouyancy.Floats, FrameworkPropertyMetadataOptions.AffectsArrange),
      new ValidateValueCallback(ValidateBouyancy)
    public static void SetBouyancy(UIElement element, Bouyancy value)
        element.SetValue(BouyancyProperty, value);
    public static Bouyancy GetBouyancy(UIElement element)
        return (Bouyancy)element.GetValue(BouyancyProperty);
    private static bool ValidateBouyancy(object value)
        Bouyancy bTest = (Bouyancy) value;
        return (bTest == Bouyancy.Floats || bTest == Bouyancy.Drifts || bTest==Bouyancy.Sinks);
    public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register(
Public MustInherit Class AquariumObject3
    Inherits DependencyObject
    Public Enum Bouyancy
    End Enum
    Public Shared ReadOnly BouyancyProperty As DependencyProperty = DependencyProperty.RegisterAttached("Bouyancy", GetType(Bouyancy), GetType(AquariumObject3), New FrameworkPropertyMetadata(Bouyancy.Floats, FrameworkPropertyMetadataOptions.AffectsArrange), New ValidateValueCallback(AddressOf ValidateBouyancy))
    Public Shared Sub SetBouyancy(ByVal element As UIElement, ByVal value As Bouyancy)
        element.SetValue(BouyancyProperty, value)
    End Sub
    Public Shared Function GetBouyancy(ByVal element As UIElement) As Bouyancy
        Return CType(element.GetValue(BouyancyProperty), Bouyancy)
    End Function
    Private Shared Function ValidateBouyancy(ByVal value As Object) As Boolean
        Dim bTest As Bouyancy = CType(value, Bouyancy)
        Return (bTest = Bouyancy.Floats OrElse bTest = Bouyancy.Drifts OrElse bTest = Bouyancy.Sinks)
    End Function
    Public Shared ReadOnly IsDirtyProperty As DependencyProperty = DependencyProperty.Register("IsDirty", GetType(Boolean), GetType(AquariumObject3))
End Class


DependencyObject类在其多个派生类上启用 (WPF) 属性系统服务 Windows Presentation Foundation。The DependencyObject class enables Windows Presentation Foundation (WPF) property system services on its many derived classes.

属性系统的主要功能是计算属性的值,并提供有关已更改的值的系统通知。The property system's primary function is to compute the values of properties, and to provide system notification about values that have changed. 参与属性系统的其他键类为 DependencyPropertyAnother key class that participates in the property system is DependencyProperty. DependencyProperty 允许将依赖属性注册到属性系统,并提供有关每个依赖属性的标识和信息,而 DependencyObject 作为基类使对象可以使用依赖项属性。DependencyProperty enables the registration of dependency properties into the property system, and provides identification and information about each dependency property, whereas DependencyObject as a base class enables objects to use the dependency properties.

DependencyObject 服务和特征包括:DependencyObject services and characteristics include the following:

  • 依赖属性承载支持。Dependency property hosting support. 通过调用 Register 方法,并将该方法的返回值存储为类中的公共静态字段,来注册依赖属性。You register a dependency property by calling the Register method, and storing the method's return value as a public static field in your class.

  • 附加属性托管支持。Attached property hosting support. 可以通过调用方法来注册附加属性 RegisterAttached ,并将该方法的返回值存储为类中的公共静态只读字段。You register an attached property by calling the RegisterAttached method, and storing the method's return value as a public static read-only field in your class. (还有其他成员要求;请注意,这表示附加属性的 WPF 特定实现。(There are also additional member requirements; note that this represents a WPF specific implementation for attached properties. 有关详细信息,请参阅 附加属性概述。然后可以在派生自的任何类上设置附加属性 DependencyObject ) 。For details, see Attached Properties Overview.) Your attached property can then be set on any class that derives from DependencyObject.

  • 获取、设置和清除中存在的任何依赖项属性值的实用工具方法 DependencyObjectGet, set, and clear utility methods for values of any dependency properties that exist on the DependencyObject.

  • 元数据、强制值支持、属性更改通知和替代依赖属性或附加属性的回调。Metadata, coerce value support, property changed notification, and override callbacks for dependency properties or attached properties. 此外, DependencyObject 类还有助于依赖项属性的每个所有者的属性元数据。Also, the DependencyObject class facilitates the per-owner property metadata for a dependency property.

  • 派生自、或的类的公共 ContentElement 基类 Freezable VisualA common base class for classes derived from ContentElement, Freezable, or Visual. (UIElement ,另一个基元素类具有包含的类层次结构 Visual 。 ) (UIElement, another base element class, has a class hierarchy that includes Visual.)



初始化 DependencyObject 类的新实例。Initializes a new instance of the DependencyObject class.



获取 DependencyObjectType 包装此实例的 CLR 类型的。Gets the DependencyObjectType that wraps the CLR type of this instance.


获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(继承自 DispatcherObject)

获取一个值,该值指示此实例当前是否为密封的(只读)。Gets a value that indicates whether this instance is currently sealed (read-only).



确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)

清除属性的本地值。Clears the local value of a property. 要清除的属性由 DependencyProperty 标识符指定。The property to be cleared is specified by a DependencyProperty identifier.


清除只读属性的本地值。Clears the local value of a read-only property. 要清除的属性由 DependencyPropertyKey 指定。The property to be cleared is specified by a DependencyPropertyKey.


对指定依赖属性的值进行强制。Coerces the value of the specified dependency property. 通过对调用方 DependencyObject 上存在的依赖属性的属性元数据中所指定的任何 CoerceValueCallback 函数进行调用来完成此操作。This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.


确定提供的 DependencyObject 是否等效于当前 DependencyObjectDetermines whether a provided DependencyObject is equivalent to the current DependencyObject.


获取此 DependencyObject 的哈希代码。Gets a hash code for this DependencyObject.


创建一个专用的枚举数,用于确定哪些依赖项属性在此 DependencyObject 上具有以本地方式设置的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.


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

(继承自 Object)

DependencyObject 的此实例返回依赖属性的当前有效值。Returns the current effective value of a dependency property on this instance of a DependencyObject.


重新评估指定依赖属性的有效值。Re-evaluates the effective value for the specified dependency property.


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

(继承自 Object)

每当更新此 DependencyObject 的任何依赖属性的有效值时调用。Invoked whenever the effective value of any dependency property on this DependencyObject has been updated. 更改的特定依赖属性将在事件数据中报告。The specific dependency property that changed is reported in the event data.


如果存在,则返回依赖属性的本地值。Returns the local value of a dependency property, if it exists.

SetCurrentValue(DependencyProperty, Object)

设置依赖属性的值而不更改其值源。Sets the value of a dependency property without changing its value source.

SetValue(DependencyProperty, Object)

设置依赖属性的本地值,该值由其依赖属性标识符指定。Sets the local value of a dependency property, specified by its dependency property identifier.

SetValue(DependencyPropertyKey, Object)

设置一个只读依赖属性的本地值,该值由依赖属性的 DependencyPropertyKey 标识符指定。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.


返回一个值,该值指示序列化进程是否应序列化所提供的依赖属性的值。Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.


返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)